I am getting syntax error while seting variables . Can some one please tell me where i am doing wrong.
#echo off
setlocal EnableDelayedExpansion
cd C:\data
for %%i in (*.pgp)
do
(
set encrypted=%%i
set decrypted=!encrypted:.gpg=!
gpg --batch --yes --passphrase "xyz" -o !decrypted! --decrypt !encrypted!
)
endlocal
if i do the same logic with out seting any variables it works
for %%i in (*.pgp)
do
(
must be coded as
for %%i in (*.pgp) do (
ie. the do and the ) and the ( after the do must all be on the same physical line.
Also, in your replace set, have you specified .gpg in place of .pgp?? (in which case, %%~ni could be used in place of the substitution.
in the gpg line, perhaps you need to quote the decrypted and encrypted strings, or you could possibly use "%%~ni" and "%%i" respectively.
Related
While running something like .bat, the "X:\..\..path" often becomes ""X:\..\path and producing errors. For example, I was installing apktool, then it just appeared this:
'""C:\Program' is not recognized as an internal or external command,
operable program or batch file.
I then copy the command and put one of the double quote to the end, which is like this: "C:\Program"
And everything just went smoothly, installation was successful. Then I tried to decode an apk, and the exactly same problem occurred: '""C:\Program' is not recognized as an internal or external command, operable program or batch file. This time I have no idea how to fix it, it's not like the .bat now, I cannot get the #echo on and copy the last command and edit it. So I am here to ask: If I am the only one who met this? Any way to fix this? Thank you.
My command:
apktool d test.apk
Image of running a decode command : 1
apktool.bat content:
#echo off
setlocal
set BASENAME=apktool_
chcp 65001 2>nul >nul
set java_exe=java.exe
if defined JAVA_HOME (
set java_exe="%JAVA_HOME%\bin\java.exe"
)
rem Find the highest version .jar available in the same directory as the script
setlocal EnableDelayedExpansion
pushd "%~dp0"
if exist apktool.jar (
set BASENAME=apktool
goto skipversioned
)
set max=0
for /f "tokens=1* delims=-_.0" %%A in ('dir /b /a-d %BASENAME%*.jar') do if %%~B gtr !max! set max=%%~nB
:skipversioned
popd
setlocal DisableDelayedExpansion
rem Find out if the commandline is a parameterless .jar or directory, for fast unpack/repack
if "%~1"=="" goto load
if not "%~2"=="" goto load
set ATTR=%~a1
if "%ATTR:~0,1%"=="d" (
rem Directory, rebuild
set fastCommand=b
)
if "%ATTR:~0,1%"=="-" if "%~x1"==".apk" (
rem APK file, unpack
set fastCommand=d
)
:load
"%java_exe%" -jar -Duser.language=en -Dfile.encoding=UTF8 "%~dp0%BASENAME%%max%.jar" %fastCommand% %*
rem Pause when ran non interactively
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.
set java_exe="%JAVA_HOME%\bin\java.exe"
Should be
set "java_exe=%JAVA_HOME%\bin\java.exe"
(apply this principle throughout your code)
Then, if you require " anywhere, insert it where it's needed - don't try to include it as part of a variable's value.
This should clean up at least some of your problems.
I write two for loops to do data automation. While variables echoed well in each loop, the last step (data process using a well-written batch) keeps giving errors that variables set previous do not exist.
The code loops through the subfolders (q1, q2, etc.) under the directory. For each subfolder, there is another for loop to set several variables. I echoed three variables fine in loops.
However, when using a batch called abc.rb, the error is COM_M does not exist.
Actually, the error is all three variables do not exist.
setlocal ENABLEDELAYEDEXPANSION
for /f %%f in ('dir /ad /b ') do (
echo %%f
pause
pushd %%f
for %%a in (*.a*.dat) do (
set COM_DATA=%%a
echo !COM_DATA!
set COM_V=%%f\com-v.dat
echo !COM_V!
set COM_M=%%f\com-M.dat
echo !COM_M!
)
chdir
set fig=someA
set matrix=someB
rem use a written batch (called abc.rb) to process data
abc.rb -a !COM_DATA! -b !COM_V! -c !COM_M! -d !fig! -e !matrix!
popd
)
endlocal
Can anyone find any bugs? Thank you!
I am not sure why the need to pushd into the dir, but as far as I can see, there is only a need for a single for loop:
#echo off
setlocal enabledelayedexpansion
set "fig=someA"
set "matrix=someB"
for /R %%a in (*.a*.dat) do (
set "COM_DATA=%%a"
echo !COM_DATA!
set "COM_V=%%~dpacom-v.dat
echo !COM_V!
set COM_M=%%~dpacom-M.dat
echo !COM_M!
rem If abc.rb is is NOT a windows batch file, remove call below
call abc.rb -a "!COM_DATA!" -b "!COM_V!" -c "!COM_M!" -d !fig! -e !matrix!
)
If you require the pushd (which I doubt)
#echo off
setlocal enabledelayedexpansion
set "fig=someA"
set "matrix=someB"
for /R %%a in (*.a*.dat) do (
pushd "%%~dpa"
set "COM_DATA=%%a"
echo !COM_DATA!
set "COM_V=%%~dpacom-v.dat"
echo !COM_V!
set "COM_M=%%~dpacom-M.dat"
echo !COM_M!
rem If abc.rb is is NOT a windows batch file, remove call below
call abc.rb -a "!COM_DATA!" -b "!COM_V!" -c "!COM_M!" -d !fig! -e !matrix!
popd
)
The double quotes will help if the paths have whitespace, if your program has an issue with them, then you can remove them: abc.rb -a !COM_DATA! -b !COM_V! -c !COM_M! -d !fig! -e !matrix!
#echo off
setlocal ENABLEDELAYEDEXPANSION
set "fig=someA"
set "matrix=someB"
set "COM_V=com-v.dat"
set "COM_M=com-M.dat"
for /f %%f in ('dir /ad /b') do (
echo %%f
pause
if exist "%%~f\*.a*.dat" (
pushd "%%~f" && (
for %%a in (*.a*.dat) do (
set "COM_DATA=%%~a"
echo !COM_DATA!
)
chdir
rem use a written batch called abc.rb to process data
call abc.rb -a "!COM_DATA!" -b "!COM_V!" -c "!COM_M!" -d "!fig!" -e "!matrix!"
popd
)
)
)
endlocal
Issues:
If the nested for loop finds no files with the matched pattern of *.a*.dat, then the variables COM_DATA, COM_V and COM_M may not be defined or updated with a newer value.
Value of COM_DATA is a filename. Values of COM_V and COM_M is the parent folder name and filename, which is inconsistent. Based on the current directory, I would consider filenames as correct. This means that COM_V and COM_M never need to change.
If abc.rb is a batch-file, then you need to use call for the interpreter to return control back to the main script.
Changes:
Test if the file pattern exists, and run the code within the code block if true.
COM_V and COM_M moved out of the for loop as values never change.
Calling abc.rb as being a batch-file.
fig and matrix moved out of the for loop as values never change.
Double quote setting of variables and use of variables to avoid issues with spaces, special characters etc.
pushd && ( ensures the code within the parentheses is run only on success of changing directory.
Removed parentheses in the rem line. They may not cause a problem, though rem lines are parsed and can cause a syntax error. Suggest avoiding special characters in rem lines unless you intend to debug.
I have a batch checking for a specific file like this:
if exist "C:\Program Files\App\App version number\file.exe" (
echo EXISTS
) else (
echo NOT EXIST!
)
I want to be able to check for the file.exe in any version so it might be in:
"C:\Program Files\App\App v.1\file.exe"
"C:\Program Files\App\App v.2\file.exe"
How can I skip the version number as a requirement inside the path?
Here is an example using the WHERE command with conditional operators.
where /Q /R "C:\Program Files\App" file.exe && echo found || echo not found
Might be a bit overkill but should work.
main.bat:
#echo off
for /f "tokens=*" %%i in ('sub.bat') do (
if "x%%i"=="x" (
echo "Not found!"
) ELSE (
echo "Found in %%i"
)
)
sub.bat:
#echo off
set your_path="C:\Your Path\"
cd /d %your_path%
for /r %%p in (file.exe) do echo %%p
Explanation:
The main batch is the one to execute. It reads the output of the sub batch and reacts properly.
Set the root for the search to the variable your_path. It will change the directory and then searches with an /Recursive loop through the current directory and looks for the file you set in the parenthesis. If the file is found, it will echo that to get read by the main file.
Feel free to ask further questions :)
setlocal
set AppDir=
for /d %%d in ("C:\Program Files\App\App v*") do if exist "%%d\file.exe" set "AppDir=%%d"
if defined AppDir (
echo EXISTS - it's in "%AppDir%"
) else (
echo NOT EXIST!
)
endlocal
(Update: Edited to put quotes around variables in case there's a filename that contains a special character such as &. Thanks to #aschipfl for pointing this out. On the other hand, there's no need to use %%~d instead of just %%d because %%d won't have any quotes anyway.)
This for command will not accept the file extension. All i get is that it cant find "myth." Any ideas?
Code:
#echo off
goto s2
:s
for /f %%h in (\\WTAB-14R2S02\Users\100048201\Documents\Chat\Program_Files\last\%un%.last) do (
set currentchannel=%%h
)
pause
title /%currentchannel%
cls
ECHO ---------------Chat Messages---------------
TYPE \\WTAB-14R2s02\Users\100048201\Documents\Chat\Program_Files\channels\%currentchannel%.channel
ping localhost -n 2 >nul
goto s
:s2
set un=myth
goto s
set un=myth
^ there is a space here
The space at the end is included in the variable value. Remove it or still better use quotes to prevent this problem
set "un=myth"
Or, if the space is really needed (and it will still be better to use quotes to better see it in the code), then you need to include the quotes to the file reference in for /f command
for /f "usebackq" %%h in (
"\\WTAB-14R2S02\Users\100048201\Documents\Chat\Program_Files\last\%un%.last"
) do (
...
Note that usebackq has been included to avoid the quoted file being treated as a direct string to be processed.
I would like use command batch to create list folder.
But the name of folder include symbol characters, I don't know how to use cmd batch
Ex: I would like create list folder
01.DOCUMENT
02.SOURCE
03.DESIGN
04.TEST
05.REPORT
06....
Input: Input root-directory(strfolder)
Then example use: mkdir %strfolder%\ (+ name folder 01.Ducment,...) but I don't know use
My current code:
#echo off
cls
set /p folderName=Enter project name:%=%
#echo %folderName%
SET mypath=%~dp0
rem echo %mypath:~0,-1%
set folder=%mypath%%folderName%
if exist %folder% (
echo "Folder already exists"
) else (
mkdir %folder%
rem mkdir %folder%\%1 %RELEASE% --> this line don't know how :D
echo %folder%\%^1%
)
pause
You'd need to quote your target if it contains symbols:
mkdir "%folder%"
(md is the same as mkdir)
Also:
mkdir "%folder%" 2>nul
will create the directory; the 2>nul suppresses the error message.
%release% appears from nowhere. You don't say what it is or where it comes from; then you REM it anyway. No iea of what is happening there.
Now if your directory-names are in a file then
for /f "delims=" %%i in (filename.txt) do echo %%i
should show you the names. If you want to create subdirectories using these names then
for /f "delims=" %%i in (filename.txt) do echo MD "%folder%\%%i"
should do that for you - well, would ECHO the command; remove the ECHO keyword to actually make the directory.
Note that virtually any character would be happy so long as it remains in the metavariable %%i used as a loop-control. Some less-frequently-used characters can present a problem within an ordingary %variable%.
If your data contains parentheses, then the parser may become confused about whether a parenthesis is part of the command or the data. Best to avoid parenthesised constructs if that is the case.