How to get %s recognized in a .bat file? - batch-file

I have a 3-command .bat file that runs fine as 3 separate commands (entered 1 at a time into command line) but does not work when I put them as 3 lines in a bat file.
Here's the file:
mogrify -format pgm ephemeral:*.png
pause
CALL forfiles /M *.pgm /C "cmd /c for %s in (#fname) do DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i %s.pgm -o %s.ppm
pause
mogrify -format png ephemeral:*.ppm
The first command runs fine, but the command line gives me this error for the second command:
s.pgm was unexpected at this time
The reason I have that for loop with fname and %s is because the debayer.exe program doesn't understand #fname.pgm as a filename, but it does realize that's means bring in a string, so it does that, and then forfiles recognizes #fname and replaces it with the current file name.
Any ideas on how to solve this problem?
I have tried:
Putting %% instead of %
Putting a ^ infront of each %
Adding the CALL that is in the above code
Adding semicolons to the end of each line.

First remove the CALL it's useless here and it will start the parse a second time so it will remove percent signs.
And double the percent signs, as they will be halfed one time.
forfiles /M *.pgm /C "cmd /c for %%s in (#fname) do DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i %%s.pgm -o %%s.ppm

You also don't need forfiles. A straight forward for loop will work.
mogrify -format pgm ephemeral:*.png
pause
for %%s in (*.pgm) do DebayerGPU.exe -demosaic DFPD_R -CPU -pattern GRBG -i "%%s" -o "%%~ns.ppm"
pause
mogrify -format png ephemeral:*.ppm

Related

unable to delete the directory while executing a batch via cmd

I'm having trouble deleting the sub-directory, while I'm executing the following batch via cmd. The mentioned dir is getting deleted, and I'm getting the relevant log file of the deleted files, but the sub-dir isn't getting deleted. Since it ain't getting deleted, I don't have a proper log file.
Here is the code that I'm executing,
#echo off
SETLOCAL
:: set folder path
set dump_path=%1
:: set min age of files and folders to delete
set max_days=%2
shift
shift
:: remove files from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c del /q #path && echo #path>>logfile.log"
:: remove subdirectories from %dump_path%
forfiles -p %dump_path% -m *.* -d -%max_days% -c "cmd /c IF #isdir == TRUE rd /S /Q #path && echo #path>>logfile.log"
Any help is appreciated.
Below is the pic, in that further, I added a folder with some contents to check, but it ain't working.
More info, as it still ain't working, any way to change it into a PowerShell execution?
Below pic, before executing the cmd,
Post execution of the cmd,
Still, I'm unable to understand why it isn't removing the sub-dir files...This time, I removed the sub-dir cmd from my batch and added a '/S' to my first cmd.
There are two problems with your batchfile:
You mention you want to remove the files with are older than 15 days, but in your batchfile you remove the files which are more recent than 15 days (there's a minus in front of the %max_days%, not a plus, you need to change that).
You mention launching the batchfile, using 15>nul as a second parameter, which is wrong. It should simply be 15.

Call MSXSL from batch file

I want to create a batch file that loops through a folder containing xml files, then call msxsl to modify them and after modify the xml file, copying to another folder with original filename.
I tried this:
forfiles /p C:\Users\mae\Documents\Testing\MSXSL\In /m *.xml /c "cmd /c C:\Users\mae\Documents\Testing\MSXSL\msxsl.exe #file pre-process_add_filename.xsl -o C:\Users\mae\Documents\Testing\MSXSL\Out\#file"
But that gives me this error:
Error occurred while creating file 'C:\Users\mae\Documents\Testing\MSXSL\Out\"bk_OIOUBLInvoice_TEST.xml"'.
Code: 0x8007007b
The filename, directory name, or volume label syntax is incorrect.
This is because of the double quotes around the output filname. How do I get around this?
As already suggested by others in comments, you should use a standard for loop for your task rather than forfiles:
for %%I in ("%UserProfile%\Documents\Testing\MSXSL\In\*.xml") do (
"%UserProfile%\Documents\Testing\MSXSL\msxsl.exe" "%%I" "pre-process_add_filename.xsl" -o "%UserProfile%\Documents\Testing\MSXSL\Out\%%~nxI"
)
But if you do insist on forfiles you could use the following code:
forfiles /P "%UserProfile%\Documents\Testing\MSXSL\In" /M "*.xml" /C "cmd /C for %%I in (#file) do 0x22%UserProfile%\Documents\Testing\MSXSL\msxsl.exe0x22 #file 0x22pre-process_add_filename.xsl0x22 -o 0x22%UserProfile%\Documents\Testing\MSXSL\Out\%%~I0x22"
The inner for loop together with the ~-modifier is used to get rid of the additional quotation marks around the file name returned by #file. The term 0x22 is forfiles-specific and marks a literal quotation mark.

How to substring the last slash of a directory string in batch file?

I wanted to remove the last forward slash of a directory string so that I use the same path for forfiles tool to delete old files. I have tried the solution here but it didn't work. The script saves the output to a text file so that I see the result. The forfiles doesn't work as the directory is incorrect format.
What I want to just to remove the last backward slash "C:\Database\Backup\".
The runable code below is what I have attempted. The problem is line 6, which outputs "C:\Database\Backup\" :~0,-1 instead of "C:\Database\Backup"
set BACKUP_DIR="C:/Database/Backup/"
set LOG_FILE=%BACKUP_DIR%log_file.txt
if not exist BACKUP_DIR mkdir %BACKUP_DIR%
set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\%
echo %BACKWARD_SLASH_DIR% > %LOG_FILE%
set DELETE_DIR=%BACKWARD_SLASH_DIR%:~0,-1%
echo %DELETE_DIR% >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c echo Deleted #file" >> %LOG_FILE%
forfiles -p %DELETE_DIR% -s -m *.* -d -1 -c "cmd /c del #path"
This is a good example of why you should always set "variable=value" with the "variable=value" pair quoted. That way, the quotation marks aren't part of the variable value, and you can explicitly quote "%variable%" when necessary. It also removes ambiguity and ensures that there's no trailing whitespace inadvertently added to the end of the value. Matter of fact, in the code you pasted, there is an extra space at the end of your set BACKWARD_SLASH_DIR=%BACKUP_DIR:/=\% line.

findstr or grep output to a file

Im in Windows Server 2003 and using below commands to fetch string matching lines in files created today in a specific directory.
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c findstr /i /c:\"Hey Hi\" #FILE" >> txt5.txt
or
forfiles -p D:\ -m *.1 -d +0 -c "cmd /c grep \"Hey Hi\" #FILE" >> txt5.txt
Geeting error 'FINDSTR: Write error' and 'grep write error bad file descriptor' respectively for both commands. So basically the commands work i.e. display the output in screen but unable to redirect the output to a file.
I did not find suitable solution though users reported this same error for different scenarios. Any help is appreciated. Thanks in advance!
You should be able to remove the cmd /c; I don't see any reason you'd need a new copy of the command shell open for the findstr call.
This works for me correctly at the command prompt:
forfiles -m t*.xml -d +0 -c "findstr /i "Item" #file" >> out.txt.
It produces an out.txt file that contains the proper content matching the search criteria.

If Batch File Modified Date is older than 90 days, delete files

Basically what I'm trying to do is create a batch file and place it in my startup that will use the modified date of the same batch file and see if it is greater or less than 90 days old. If it isn't, nothing happens and life goes on, but if it is, I want it to delete the contents of my downloads folder. I have a little bit of coding, but I've been testing it, but for some reason it isn't wanting to work for me. Any help would be greatly appreciated.
forfiles -p "C:\LOCATION OF .BAT\" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
I'm just starting out using batch commands and would really love the help
This works for you if you are using Win2003 or WinXP and have forfiles.exe installed on your machine...
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat -d -90 -c "cmd /c del C:\user folder\Downloads"
Later versions of Windows and Windows Server have it installed by default.
For Win7 or higher: Syntax has changed a little therefore the updated command is:
forfiles -p "C:\path_of_your_bat" -s -m rmdownload.bat /D -90 /C "cmd /c del C:\user folder\Downloads"
Assuming your target path contains spaces etc, then use this in your forfiles command. The 0x22 represents a double quote character.
If it echos the command then try it without the echo to actually perform the deletion
"cmd /c echo del 0x22C:\user folder\Downloads\*.*?0x22"

Resources