I want to have this script execute another script if a file name starts with one thing, another if it starts with another thing, and to continue on through the rest of the code if it starts with anything else. I want it to iterate through all the files in a folder and check against this. I have been working on it for a while and am looking for a fresh set of eyes since apparently I can't seem to get it working properly. Any help would be appreciated!
:========================================================================================
:: purpose: 1. copy file(s) from the export dir (SOURCE_DIR) (ARCHIVE_DIR)
:: 2. copy file(s) from the export dir (SOURCE_DIR) (TARGET_DIR)
:: 3. once file(s) are processed delete them from the export dir (SOURCE_DIR)
::=======================================================================================
::
#echo off
setlocal
:: Display the command line on the window's Title Bar
title %0 %1
set LOG_FILE="here.txt"
set SOURCE_DIR=here\there
set TARGET_DIR=here\nowHere
set ARCHIVE_DIR=here\oldStuffnThings
set FILE=FileStartingWithBlahBlergOrOther.docx
echo --------------------------------------------------------------------------------------------------------------------------------------- >> %LOG_FILE%
echo %DATE% %TIME% Starting file move >> %LOG_FILE%
if not exist "%SOURCE_DIR%%FILE%" (
echo %DATE% %TIME% "%SOURCE_DIR%%FILE%" not found >> %LOG_FILE%
echo %DATE% %TIME% no files to move so stopping >> %LOG_FILE%
exit
)
FOR /R %completepath% %%G IN (FileStartingWithBLAH*) DO (
call C:\otherScript1
)
FOR /R %completepath% %%G IN (FileStartingWithBLERG*) DO (
call C:\otherScript2
)
:: one at a time copy files to archive, copy to dir, then delete from the export dir
FOR /R %SOURCE_DIR% %%G IN (%FILE%) DO (
echo %DATE% %TIME% %%G was found for processing >> %LOG_FILE%
XCOPY %%G "%ARCHIVE_DIR%"/Y
echo %DATE% %TIME% %%G was archived status %ERRORLEVEL% >> %LOG_FILE%
XCOPY %%G "%TARGET_DIR%"/Y
echo %DATE% %TIME% %%G was copied to dir status %ERRORLEVEL% >> %LOG_FILE%
del %%G
echo %DATE% %TIME% %%G was deleted from the export dir status %ERRORLEVEL% >> %LOG_FILE%
)
rem set err_code=%errorlevel%
rem if %err_code% NEQ 0 (
rem echo %DATE% %TIME% **ERROR moving files %err_code% >> %LOG_FILE%
rem exit
rem )
rem echo %DATE% %TIME% File(s) moved successfully >> %LOG_FILE%
rem echo %DATE% %TIME% Process Complete >> %LOG_FILE%
A problem is that you have to put a backslash after SOURCE_DIR=here\there or it will look for here\thereFileStartingWithBlahBlergOrOther.docx
Related
I'm trying to create a small script that looks for a specific file in an hidden folder and remove it using IF ELSE, but it doesn't seem to work. It works when I try to find and delete a local file, but not when I try a remote hidden file.
This is my code:
#echo off
set /p destination="Enter destination PC name: "
#echo off
set /p username="username: "
echo.
echo [33m'Removing 'notes.ini' file[0m
timeout /T 2 /NOBREAK > nul
echo.
if exist "\\%destination%\C$\Users\%username%\AppData\Local\Lotus\Data\notes.ini" (
del /A /Q /F "\\%destination%\C$\Users\%username%\AppData\Local\Lotus\Data\notes.ini"
echo [92m'Notes.ini' cleared.[0m
) else (
echo [33m'Notes.ini' doesn't exist.[0m
)
echo.
timeout /T 2 /NOBREAK > nul
echo [92mScript clompleted[0m
I tried with a local file, and then the scripts works, but not with a remote hidden file. I always get the "else" statement as return with hidden files
What I want the .bat to do is to "scan" the files, in this case, .mkv, looking for streams (if there's more than a video stream and an audio stream you might need a matroska file). If you have an audio stream and a video stream, let's change the files as there's no reason to use matroska.
Currently I have 10 seasons of a TV show, which I personally ripped from my own DVDs, and they are in .mkv format. But inside the .mkv there's only video and audio, the subtitles are separate.
The only reason I want to change .mkv to .mp4 is that plex can't use .mkv.
The current code I have to remove metadata on all .XXX in the folder:
if not exist "%~dp1_temp" (
goto foldercreate
)
else (
goto continue
)
:foldercreate
echo.
echo Created temp folder.
echo -
mkdir _temp
attrib _temp +h
set errorlevel=
:continue
for %%f in (*%~x1) do (
title Fixing %%f...
move "%%f" _temp >nul
echo Currently fixing %%f...
ffmpeg -xerror -hide_banner -i "%~dp1_temp\%%f" -map_metadata -1 -codec copy -map 0 "%~dp1%%f" -loglevel warning -stats
if %errorlevel% neq 0 (
pause
)
del "%~dp1_temp\%%f"
echo -
)
if %errorlevel% neq 0 (
echo FFmpeg errored...
pause
)
echo All done! removing temp dir...
rmdir _temp /S /Q
timeout /t 2 /nobreak>nul
:eof
I am trying to insert an "if not exist" in a windows batch file where another similar IF with the same formatting IS working - can't tell why this one fails after researching and testing.
The second If not exist working as expected When the first is REMd out
Formatting is the same, %INIFile% is defined
#echo off
setlocal EnableExtensions Enabledelayedexpansion
set "TODAY=%Date:~4,2%-%Date:~7,2%-%Date:~12,2%"
set "NOW=%time:~0,2%.%time:~3,2%.%time:~6,2%"
set "TempFile=%TEMP%\%~n0.tmp"
set "INIFile=Parameters_INI.ini"
if not exist ".\%INIFile%" (
echo ERROR: List file "%INIFile%" not found.
echo ERROR: List file "%INIFile%" not found.>>%LogFile%
goto :EndBatch
)
:: Get Parameters
call :get-ini %INIFile% Parameters ListFile result
Set "ListFile=%result%"
call :get-ini %INIFile% Output LogName result
Set "LogFile=%result%_%EntryName%_%TODAY%_T%NOW%_Log.txt"
Echo INI File Updater
Echo PC List: %ListFile%
Echo PC List: %ListFile%>>%LogFile%
if not exist ".\%ListFile%" (
echo ERROR: List file "%ListFile%" not found.
echo ERROR: List file "%ListFile%" not found.>>%LogFile%
goto :EndBatch
)
goto :EndBatch
:get-ini <filename> <section> <key> <result>
set %~4=
set insection=
for /f "usebackq eol=; tokens=*" %%a in ("%~1") do (
set line=%%a
if defined insection (
for /f "tokens=1,* delims==" %%b in ("!line!") do (
if /i "%%b"=="%3" (
endlocal
set %~4=%%c
goto :eof
)
)
)
if "!line:~0,1!"=="[" (
for /f "delims=[]" %%b in ("!line!") do (
if /i "%%b"=="%2" (
set insection=1
) else (
endlocal
if defined insection goto :eof
)
)
)
)
:EndBatch
endlocal
pause
Parameters_INI.ini
[Parameters]
ListFile=PCList.txt
Target=SMSStart.ini
TarDIR=Storeman
SectionName=[Maintenance]
EntryName=Reboot
NewValue=1
[Output]
LogName=INI_Update
PCList.txt
LAB-LANE005
LAB-LANE006
LAB-LANE001
LAB-LANE007
LAB-LANE008
echo ERROR: List file "%ListFile%" not found.>>%LogFile%
You don't appear to have ever set LogFile to a value, so if it's not set, this line will fail when it's parsed as >> on it's own at the end of the line is invalid syntax, even if it's not run.
If you set LogFile before the batch file, or in the first lines of your batch file, the rest should work.
Ok, I'm trying to do a couple nested IF EXIST statements to check for the presense of a couple folders. If the first folder exists, set Folder1 to equal 1, and then skip to Install. Same with the Folder2, and then if neither folder exists just skip to install.
But even when Folder1 doesn't exist, this still sets %Folder1% to equal 1. What am I missing/not doing?
Thanks!
if exist "c:\folder1" set Folder1=1
echo %Folder1%
goto install
else if exist "c:\folder2" set Folder2=1
echo %Folder2%
goto Install
else goto Install
:Install
Two fundamental problems:
A compound statement must be parenthesised.
Within parentheses, changing a variable value will NOT be visible UNLESS you have executed a SETLOCAL ENABLEDELAYEDEXPANSION - and even then you'd need to use !var! not %var%
So:
SETLOCAL ENABLEDELAYEDEXPANSION
if exist "c:\folder1" (
set Folder1=1
echo !Folder1!
goto install
) else if exist "c:\folder2" (
set Folder2=1
echo !Folder2!
goto Install
) else goto Install
:Install
Or preferably,
#ECHO off
if exist "c:\folder1" (
set Folder1=1
goto install
) else if exist "c:\folder2" (
set Folder2=1
goto Install
) else goto Install
:Install
SET folder
Or even simpler
#ECHO off
if exist "c:\folder1" set Folder1=1&goto install
if exist "c:\folder2" set Folder2=1&goto Install
:Install
SET folder
Test:
#ECHO OFF
setlocal
SET "folder1="
SET "folder2="
ECHO.----------No folders
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Folder 1 only
MD c:\folder1
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Folder 2 only
RD c:\folder1
MD c:\folder2
DIR /b /ad c:\folder*
CALL :test
ECHO.----------Both
MD c:\folder1
DIR /b /ad c:\folder*
CALL :test
RD c:\folder1
RD c:\folder2
GOTO :eof
:test
if exist "c:\folder1" set Folder1=1&goto install
if exist "c:\folder2" set Folder2=1&goto Install
:Install
SET folder
SET "folder1="
SET "folder2="
GOTO :eof
This test creates and deletes the two directories in question.
Here's the result:
----------No folders
----------Folder 1 only
folder1
Folder1=1
----------Folder 2 only
folder2
Folder2=1
----------Both
folder1
folder2
Folder1=1
Note that
SET "folder1="
SET "folder2="
Which is executed both at the start and after each report ensures that the environment variables in question are removed from the environment to prevent the code giving false results on stale information.
if exist "c:\folder1" (
set Folder1=1
echo %Folder1%
goto install
) if exist "c:\folder2" (
set Folder2=1
echo %Folder2%
goto Install
) else goto Install
:Install
This code doesn't set %folder1%=1 if folder1 doesn't exists, and it produces no output in this case. If Folder1 doesn't exists AND Folder2 exists THEN %folder2% is set to 1, otherwise %folder2% is empty. Put an unclosed left parentheses after the echocommand to suppress the output, if a variable is empty.
#echo off &setlocal
if exist "c:\folder1" set "Folder1=1"
echo(%Folder1%
if not defined Folder1 if exist "c:\folder2" set "Folder2=1"
echo(%Folder2%
goto Install
:Install
endlocal
I am testing some small scripts that I have. This particular one is driving me crazy. In my mind it should work, but it is not doing what I thought it would/should.
#echo off
echo testing the issue
echo.
ECHO Checking the log file for errors...
echo %errorlevel%
FINDSTR /C:"open failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking1)
:TSTErrChecking1
FINDSTR /C:"does not exist" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else (GOTO TSTErrChecking2)
:TSTErrChecking2
FINDSTR /C:"Logon failed" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking3)
:TSTErrChecking3
FINDSTR /C:"Failure" C:\automatic_deployment\test1.txt
echo %errorlevel%
if %errorlevel% == 0 (
ECHO Deployment failed.
pause
GOTO quit) else ( GOTO TSTErrChecking4)
:TSTErrChecking4
FINDSTR /C:"RC (return code) = 0" C:\automatic_deployment\test1.txt
echo %errorlevel%
IF %ERRORLEVEL% == 0 (
ECHO Deployment was successful.
pause
GOTO quit) ELSE (
ECHO Deployment failed.
pause
GOTO quit)
:quit
exit
As you can see I am using "FINDSTR" to find certain words/strings from a small test1.txt file and act accordingly. I want it to say "deployment failed" and quit asap if it sees any "fail"/ "does not exist"/"open failed". If it cannot find anyone of those above and finds the "RC (return code) = 0" I want it to say it was "successful".
So, i put all those test words and strings in my test1.txt file and tested the batch script, but it keeps skipping (i think) all those fail hints/words and keeps saying it was successful. Plz, help. Thanks in advance.
it could be that you already have an %ERRORLEVEL% variable defined in your environment
Read
Raymond Chen's ERRORLEVEL is not %ERRORLEVEL% http://blogs.msdn.com/b/oldnewthing/archive/2008/09/26/8965755.aspx
and then try
FINDSTR /C:"RC (return code) = 0" test.txt
IF ERRORLEVEL 1 ECHO Deployment failed.
I just didn't have the matching strings to search for. I didn't know that FINDSTR looked for EXACTLY the same case letters when searching and comparing. That was the problem.