Part of Code in Windows Batch File Not Executing - windows

So, I don't have much experience with batch scripting and use regular programming languages quite a lot. I'm trying to write something very simple and don't understand why half of my code is not executing as I expect. Perhaps I'm breaking some cardinal rule in Windows Batch scripting. Right now the code below just makes the new directory, and then the script appears to stop and not complete any of the code involving the time, copying, or clearing of a file. Anyone see what's wrong with this code?
#echo off
rem check for directory
if not exist fileDir (
mkdir fileDir
)
rem check for file...
if file.txt exists (
REM get computer name
set hostName = echo %computername%
rem get time format
SET HOUR=%time:~0,2%
SET dtStamp9=%date:~-4%%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%%time:~6,2%
SET dtStamp24=%date:~-4%%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%%time:~6,2%
if "%HOUR:~0,1%" == " " (SET dtStamp=%dtStamp9%) else (SET dtStamp=%dtStamp24%)
type file.txt >> ./fileDir/%hostName%_%dtStamp%.LOG
REM clearing file contents of file.txt.
break > file.txt
)

The line:
if file.txt exists (
is not a valid batch file command. There is not exists option for if the option is exist singular and it must go before the file name. So the line should be:
if exist file.txt (
You can get more information on using if in a batch file by typing help if in a command prompt window.

Related

Batch file locating duplicate patterns in filename then process

Ok, i've been working on a batch file for some time now, and im just stuck on the last bit.
What im trying to accomplish is to loop through a directory, create a variable which stores the filename of each file in the directory without the extension. Then for each file in the first loop, loop through a different directory and try to find any filename in the second loop that has the same name as stored in the variable, and then just output some simple text.
So for instance lets say in the first directory there is a filename called imafile-yehyeh.png, the variable will save imafile-yehyeh, then it will loop through all the files in the second directory, and output a message for each filename that has that pattern in it, so if a file in the second directory is called imafile-yehyeh_01.mp4 or imafile-yehyeh-newtitle.jpg, they would match the pattern and a message would output.
My script is looping and i am able to echo out all the variables, the files exist as i have created them exactly as shown above, but its not echoing out the filename is set for deletion line.
Any help would be greatly appreciated. my code is as follows;
#echo off
set "parent_folder=C:\Users\Testing\script"
set "dupe_folder=DUPEFOLDER"
set "kill_folder=1 SCANNED\thumb"
setlocal enableDelayedExpansion
for %%X in ("%parent_folder%\%dupe_folder%\*") do (
set dupe_pattern=%%~nX
for %%F in ("%parent_folder%\%kill_folder%\*") do (
echo %%~nF | FIND "%dupe_pattern%" 1>NUL && (
echo %%~F is set for deletion.
)
)
)
endlocal
Thanks to #Squashman the answer was to remove the set dupe_pattern.... line
and then change the FIND command to the following;
FIND "%%~nX"
Apart from needlessly setting a variable, as already pointed out, you are also making the script inefficient. For every file in the dupe_folder you are Echoing every file name in the kill_folder and piping that into a Find command looking for matches.
Here's a simpler way of doing it, (it matches file names which begin with the same string followed by a dot, as opposed to any file name containing the string anywhere).
#Echo Off
Set "parent_folder=C:\Users\Testing\script"
Set "dupe_folder=DUPEFOLDER"
Set "kill_folder=1 SCANNED\thumb"
CD /D "%parent_folder%" 2>Nul || Exit /B
For %%A In ("%kill_folder%\*") Do If Exist "%dupe_folder%\%%~nA.*" (
Echo %%A is set for deletion.)

How to Increment a FileName in batch? (Part2)

I already tried the answer given by Mofi in my last question regarding this topic. But I changed the base name and it does not seem to work by now. If you want to see the previous question:How do I increment a filename in batch? What is wrong with this new code? It does not make a new file it just overwrites the previous made file.
:MainProcessNew
cd /D "%USERPROFILE%\Desktop"
for /F %%G in (*.json) do (
set "FileName=%%G"
set "BaseName=Device"
set "FileNumber=0"
)
:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" (
goto FileNameLoop
)
echo.>"%USERPROFILE%\Desktop\%BaseName%%FileNumber%.json"
I'm quite sure the batch code in question is not complete or reduced to a script code not really suitable to reproduce the problem because with Device.json existing in folder Desktop and no other Device*.json file existing, the empty line is first written to Device1.json. A file with name Device.json is never overwritten by the batch code in question because variable FileNumber has always at least the value 1.
Well, the FOR option /F is most likely wrong here as I suppose the FOR loop should search for *.json files as done without /F. Using a wildcard pattern like *.json together with option/F results in error message:
The system cannot find the file *.json.
Run in a command prompt window for /? or help for for help on syntax of this command.
It is completely unclear what is the purpose of the FOR loop because the FileName variable is not used at all. This variable should perhaps hold the name of last found *.json if there was any *.json file found at all. But that also does not make sense if not further used anywhere.
It is also unclear why BaseName and FileNumber are defined inside the loop and not outside.
In the complete batch code the label FileNameLoop is perhaps the beginning of a subroutine. But in the reduced batch code in question there is no call :FileNameLoop "%%G" which I would expect in this case.
So the question is hard to answer as it is unclear what is really the problem with posted batch code.
:MainProcessNew
cd /D "%USERPROFILE%\Desktop"
rem Useless FOR loop without /F commented out.
rem for %%G in (*.json) do set "FileName=%%G"
set "BaseName=Device"
set "FileNumber="
rem Skip searching for files with a file number
rem after Device if there is no Device.json file.
if not exist "%BaseName%.json" goto CreateFile
rem Otherwise keep Device.json as is and search for Device1.json,
rem Device2.json, ... until a Device*.json file with current number
rem is not found and use this number for next Device*.json file.
set "FileNumber=0"
:FileNameLoop
set /A FileNumber+=1
if exist "%BaseName%%FileNumber%.json" goto FileNameLoop
:CreateFile
rem Note: FileNumber is replaced in the line below by an empty
rem string if there is no Device.json in desktop folder.
echo.>"%USERPROFILE%\Desktop\%BaseName%%FileNumber%.json"
Hint: For debugging a batch file
comment out or remove all #echo off or echo off in the batch file or change off to on,
open a command prompt window,
enter "Path to batch file\BachFileName.bat" and press RETURN.
Now it can be seen in the command prompt window each line executed by Windows command processor after preprocessing each line and each command block which means after replacing all %VariableName% by the current value of the variable in current line or entire command block.
And error messages can be also seen as the command prompt window remains open after processing batch file stopped, except it contains the command exit without option /B which always terminates the current command process.

How to append string after file extension via batch file

Checking the integrity of zip archives recursively how can i rename bad files in a folder by appending a string after the file extension via a batch file under Windows?
I tried this script and it gives me no errors but doesn't rename bad files either.
FOR /R \backup %%A IN (*.zip) DO (
7z t "%%A" | FIND "Everything is Ok" || ((REN "%%A" "%%A.bad") & (ECHO.%%A BAD >> error.log))
)
I have experience in PHP scripting and understand this code block in general (the meaning of the pipe and the logical OR operator). However, i have absolutely no experience in Batch-scripting and adopted the script from this source. From what i understand the check receives either an "OK" or adds an entry to the log file and renames the file. The log entry is added, however the renaming is not applied. I don't understand why. Can you tell me what is going wrong and how it would have to look to work as intended?
You can use this batch code for your task.
#echo off
rem Test all ZIP files recursive from directory backup in root of
rem current drive and append .bad to those ZIP files not validate.
rem The names of those files are additionally written into an
rem error.log text file in current working directory.
for /R "\backup" %%A in (*.zip) do (
7z.exe t "%%A">nul
if errorlevel 1 (
ren "%%A" "%%~nxA.bad"
echo %%A BAD>>error.log
)
)
In help of 7-zip there is a page about Exit Codes. As it can be read on this help page any value greater 0 means something was wrong.
Therefore if errorlevel 1 can be used to check if the test on the ZIP file was positive (exit code is 0) or failed (value is greater or equal 1).
Look on help output in a command prompt window after entering if /? or help if for details about errorlevel. if errorlevel 1 means: if exit code of previous command was greater or equal 1 then ...
Further command ren requires just new file name without path as second parameter as otherwise it does not rename the file specified with or without path as first parameter. This is the reason why %%~nxA must be used as second parameter for command ren to reference just name and extension of the file to rename. Help of command for output after entering for /? or help for contains a list of those modifier letters for a file name.

Batch file if then statement from user input

Brand new here, and brand new to scripting. So, I'm struggling with this and I know I shouldn't be.
I have a batch file that currently scans and unpacks zip files. All I'm trying to do is add an input line for Business Date at the top that can then be referenced in the unpacking to make sure the dates are correct. Here is the file as it exists (I've added the user input just don't know how to reference it):
echo off
:Ask
echo Please enter Business Date (MMDDYYY)
set INPUT=
set /P INPUT=Type input: %=%
c:
cd\502
erase *.prn
7z e "\\svr-dc\ftp site\502\daily\data1.zip"
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
pause
Instead of a pause there I would like for the file to run through all the directories, they are 502 to 607, and tell me whether the filename listed in the . is the same as the user entered business date. Then, run through the rest of the directories doing the same thing. Once it's verified that I'd like to have a .txt file open with any business dates that didn't match. Does that make any sense? Any help would be appreciated.
Thanks so much.
...
c:
FOR /L %%X in (502,1,607) DO (
cd\%%X
erase *.prn
7z e "\\svr-dc\ftp site\%%X\daily\data1.zip"
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
)
You would use %input% to access the value that was entered as INPUT, but you give no clue to how "to make sure the dates are correct."
Hmm - given further information (perhaps...)
...
c:
FOR /L %%X in (502,1,607) DO (
cd\%%X
erase *.prn
7z e "\\svr-dc\ftp site\%%X\daily\data1.zip"
if exist sz%input%*.prn (
copy sz*.* sales.xls
copy sc*.* cosales.xls
copy aj*.* money.xls
copy cc*.* count.xls
) else (
echo Repoll %%X First>>C:\Users\Boster\Desktop\RePoll.txt
)
)
It's critical that the ( occurs on the same physical line as the if and that ) else ( all appears on the same physical line.
You asked a couple of question so instead of writing one batch script I thought I should break it down to explain how it would answer your questions:
1- Use %INPUT% to reference your variable/user input
2- If you will always have 502 to 607, then you can loop through them using a for loop
FOR /L %%G IN (502,1,607) DO echo %%G
3- To check if there exist a file/folder with the same value as the user input use an if EXIST %INPUT% statement
There are quite a few tutorials out there, here is one that walks you through batch file scripting

using space in path for copying files using batch script

I want to copy all the *.jar files to another directory i wrote the below script
echo Enter path to ILM_HOME:
set /p ILM_HOME=
echo Deployment in progress
copy WEB-INF/lib/*.jar "%ILM_HOME%"/webapp/WEB-INF/lib
I use C:\Documents and Settings\asimon\Desktop\test as my input
It gives me syntax of the command is incorrect
I think the problem is Documents and Settings I even put "%ILM_HOME%" and I don't need c:\Docume~1\asimon\Desktop\test any other solution?
Update
This is working
#echo off
echo
echo Enter path to ILM_HOME:
set /p ILM_HOME=
IF EXIST "%ILM_HOME%\stopApplimation.bat" (
echo Deployment in progress
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"
CALL "%ILM_HOME%\stopApplimation.bat"
CALL "%ILM_HOME%\startApplimation.bat"
) ELSE (
echo %ILM_HOME% path is incorrect.
)
also any linux solution is also helpfull with .sh for the below 2 statements
"%ILM_HOME%/stopApplimation.bat"
"%ILM_HOME%/startApplimation.bat"
for linux, how can i replace the above 2 statements?
$ILM_HOME/stopApplimation.sh
$ILM_HOME/startApplimation.sh
Did you try
xcopy WEB-INF\lib\*.jar "%ILM_HOME%\webapp\WEB-INF\lib"?
EDITED:
In your batch use CALL "%ILM_HOME%\stopApplimation.bat"

Resources