Batch script escape space not working - batch-file

When I try to run the command
C:\Windows\system32\cmd.exe /C "C:\Program Files (x86)\TorAES\certmgr.exe" -del -c -n "Certificatename" -s -r localMachine trustedpublisher
in a Windows cmd.exe I get the errormessage (translated by me):
The command "C:\Program" is misspelled or does not exist.
even other combinations with " does not work:
C:\Windows\system32\cmd.exe /C 'C:\Program Files (x86)\TorAES\certmgr.exe' -del -c -n 'Certificatename' -s -r localMachine trustedpublisher
C:\Windows\system32\cmd.exe /C "'C:\Program Files (x86)\TorAES\certmgr.exe' -del -c -n 'Certificatename' -s -r localMachine trustedpublisher"
C:\Windows\system32\cmd.exe /C \"C:\Program Files (x86)\TorAES\certmgr.exe\" -del -c -n \"Certificatename\" -s -r localMachine trustedpublisher
Unfortunately if I just run
C:\Windows\system32\cmd.exe /C "C:\Program Files (x86)\TorAES\certmgr.exe"
certmgr.exe starts but (obvious) my arguments are missing and i really need them to automatic remove my certificate when uninstalling my program.
I am absolutly unfamiliar with batchscripting.
Is someone able to find my mistake?
Thanks!

See cmd /?:
If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:
If all of the following conditions are met, then quote characters on the command line are preserved:
no /S switch
exactly two quote characters
no special characters between the two quote characters,
where special is one of: &<>()#^|
there are one or more whitespace characters between the
two quote characters
the string between the two quote characters is the name
of an executable file.
Otherwise, old behavior is to see if the first character is
a quote character and if so, strip the leading character and
remove the last quote character on the command line, preserving
any text after the last quote character.
So you can add an outer set of quotes:
C:\Windows\system32\cmd.exe /C ""C:\Program Files (x86)\TorAES\certmgr.exe" -del -c -n "Certificatename" -s -r localMachine trustedpublisher"

Related

forfile and AD homefolders doesnt work

we have an active directory with about 80 users.
what i want to do now is place in each userfolder a script that the user can run for themselves and it makes a log file in that same folder with files older than 5 days.
The homefolder for each user is mapped as drive in windows to the letter h
right now i have this:
forfiles -p "%cd%" -s -m *.* /D -5 /C "cmd /c echo #path >> %cd%\log.txt"
but it throws me this error.
H:\>forfiles -p "H:\" -s -m *.* /D -5 /C "cmd /c echo #path >> H:\\log.txt"
ERROR: Invalid argument/option - '#path'.
is there any way to solve this?
if i run in on my local pc with a test folder it works fine.
When it comes to the closing doublequote, if you move it to before your redirection it certainly would make more sense, as would changing it to > instead of >>.
The fix to your issue, is when you use the /P option, you cannot doublequote it, and also include a trailing backslash, (it's one or the other).
My reading is that the backslash is escaping the doublequote.
Because drives tend to require a trailing backslash, all you have to do is remove the doublequotes around it, (they're not needed anyhow because drives don't include spaces).
Also please remember that, as already inferred in my comment, ForFiles already uses the current directory by default so removing it entirely is easier.

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.

How to get %s recognized in a .bat 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

using sqlcmd in batch file

I've scoured StackOverflow for a few hours, and tried different suggestions for similarly asked questions, but nothing passed the parameters correctly so far (double quotes, ^).
Here's the short version of it:
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i C:\query.sql -s"," | findstr /V /C:"-" /B >c:\output.csv
Basically, I want to pass a rather long parameter containing delimiters. But the only way I see that happen is to use arguments. I'd like to keep it as simple as possible. Is this the only recourse? Can you offer an example how this might work?
Thanks in advance.
I'm not sure if it matters, but I think there should be a space between -s and ","
But more importantly, your pipe construct is wrong. Your sqlcmd command is running in a new window, but your pipe is looking for output from the START command itself in the original window - and there isn't any.
You could get your command to work by escaping the pipe and redirection.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
start /w "sqlcmd" sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," ^| findstr /V /C:"-" /B ^>"c:\output.csv"
But there is no need to use START at all. Your script can simply execute sqlcmd directly, and everything is much simpler.
#echo off
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
sqlcmd.exe -S DBserverName -U username -P p#ssword -i "C:\query.sql" -s "," | findstr /V /C:"-" /B >"c:\output.csv"
You might also be running into problems with your password, depending on what characters are used. You might have to quote and/or escape the password, and sqlcmd.exe might have its own escape rules. If it does, then you might have to worry about escaping for both cmd.exe and sqlcmd.exe.
This line should have double quotes around the entire path, in some cases it matters.
cd C:\Program Files (x86)\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\
and the start command needs double quotes at the beginning because it takes the first set as the window title.
start "" /w ....

Resources