running exe command from another directory in a for loop - windows

grep.exe has to be in the current directory in order for the below command to work:
FOR /F tokens^=4^ delims^=^" %%A IN ('grep.exe -o -P -m 1 "name=\"securitytoken\" value=\".*?\"" "forum_post_edit.html"') DO SET "securitytoken=%%A"
ECHO %securitytoken%
I tried enclosing the path to grep.exe between double quotes like "bin/grep.exe" but that messed up the syntax of the code.
Now I can use pushd and popd to temporarily switch to another directory where grep.exe is located but that affects forum_post_edit.html
How can I work around this problem?

Building on #Mofi's comment above, you can extract the short name of grep.exe using this command:
FOR /F %%A IN ("grep.exe") DO SET GrepPath=%%~fs$PATH:A
Now you can reference the short path in your command:
FOR /F tokens^=4^ delims^=^" %%A IN ('%GrepPath% -o -P -m 1 "name=\"securitytoken\" value=\".*?\"" "forum_post_edit.html"') DO SET "securitytoken=%%A"
ECHO %securitytoken%
Note: grep.exe would need to be in a folder defined in your system %PATH% directory in order for this to work.

Related

Windows command to change directory to the output of a text file [duplicate]

This question already has an answer here:
Windows cmd pass output of one command as parameter to another
(1 answer)
Closed last year.
I have this long path of the temporary folder of a project:
c:\\Users\\user\\source\\repos\\src\\etc\\even\\longer
I stored the string of text of the path in the file "path.txt" for the time being.
I would like to change directory from the windows command line like I would in Bash
$ cd `cat path.txt`
I tried with cd < type path.txt but I got.
The system cannot find the file specified.
You try to redirect the contents of a file named type to the cd command. The correct syntax would be cd < path.txt or type path.txt | cd - if cd would take input from STDIN, which it does not.
You have to read the content of path.txt to a variable and use cd with that variable.
There are two methods to read from a file: set /p and a for /f loop.
A
<path.txt set /p folder=
cd /d "%folder%"
or B
for /f "usebackq delims=" %%a in ("path.txt") do set folder=%%a
cd /d "%folder%"
or (directly processing) C
for /f "usebackq delims=" %%a in ("path.txt") do cd /d "%%a"
(All methods assuming there is just one line in the file - method A reads the first line only, method B reads all (non-empty) lines, keeping the last one in the variable and method C would cd in every folder listed in the file.)

How to source a Windows batch script

For example, I have a simple batch script get_path.bat
#echo off
echo C:\Software\dt
Also, I have another simple batch script switch_dir.bat
#echo off
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
Now, what I want to accomplish is that when I invoke in cmd.exe the batch file switch_dir.bat, my current working directory changes to C:\Software\dt.
As of now, the scripts work but they run in a process spawned from cmd.exe so my current working directory stays the same. What is missing? Basically, we need Unix-like source or . here.
Well, there are many possible solutions:
Start your script with cmd /c:
All you have to write in cmd is:
cmd /c switch_dir.bat
Using popd/pushd in your batch file:
In your switch_dir.bat add:
#echo off
pushd dir\you\want\to\remain\
get_path.bat > target.tmp
set /p TARGET=<target.tmp
cd %TARGET%
rem [code...]
popd
An additional note: A better way to find directory specified in get_path.bat is to use a for /f loop like this:
#echo off
pushd dir\you\want\to\remain\
for /f "delims=" %%A IN ('get_path.bat') do set TARGET=%%A
cd %TARGET%
rem [code...]
popd

Change to directory using variable name

I've written a script to search for a shape file (.shp), I want to change to that directory once found: Here's what I have
FOR /F %%X IN ('DIR /S /B *.shp') DO SET shapefolder=%%~DPNX
IF DEFINED shapefolder (
ECHO %shapefolder%
CD /d shapefolder REM doesn't work
) ELSE (
ECHO File not found
)
What am I missing?
Two things are wrong with your code. The first, as Oliver already pointed out, is that cd /d shapefolder should be cd /d %shapefolder% so that you're calling the actual variable.
The other thing is that SET shapefolder=%%~DPNX should beSET shapefolder=%%~DPX instead.
This is because the N refers to the filename of the file it finds, so if you have a square.shp, your code currently will look for C:\files\shapes\square instead of C:\files\shapes\ like it's supposed to.
Just add the percent sign to your variable in the cd command:
CD /d %shapefolder%

“Remote command failed with exit status 127”

I have a batch file as below:
#echo off
REM <--Fetching installed service pack version and storing it in var-->
FOR /f "tokens=* " %%a in ('findstr /I "install.servicepack" ^< "C:\A\B\C\D.properties" ') DO SET temp=%%a
SET var=%temp:~22%
REM <-- I tested, correct value is getting assigned to var say 1.2.3-->
REM <--Next, I am changing the directory using CD, in which X, Y and Z is a fixed directory path and after that it is variable based upon %var% value
cd c:\X\Y\Z\%var%
echo %cd%
REM <-- I tested and directory is correctly set against cd c:\X\Y\Z\1.2.3
REM <--With in c:\X\Y\Z\%var% (c:\X\Y\Z\1.2.3), there is an exe called uninstaller.exe and I am executing it is below:
dir
ECHO MESSAGE: Starting Silent Uninstallation of ABC Package
uninstaller.exe -options -silent
ECHO MESSAGE: Finished Silent Uninstallation of ABC Package
Set-up: I have Jenkins installed on windows and via sshexec task in ANT, I am calling the above batch file in a remote windows machine using cygwin openssh.
Issue: The above script when called from Jenkins job using above set-up, it is returning “Remote command failed with exit status 127”. However, if I am hard coding the value of %var% in cd as cd c:\X\Y\Z\a.b.c rather than passing as cd c:\X\Y\Z\%var%, script is executing fine, i.e.; directly changing the directory with the exact path (cd C:\X.Y.Z.\1.2.3).
I tried couple of ways to call uninstaller.exe after changing the directory but no success.
Please help.
Do NOT change value of TEMP variable: this is a special system variable holding the temporary directory env. variable.
Please choose another variable name.
FOR /f "tokens=* " %%a in ('findstr /I "install.servicepack" ^< "C:\A\B\C\D.properties" ') DO SET t=%%a
SET var=%t:~22%
If you change temporary directory, programs relying on it may crash (and there are a lot of them).

Batch script to execute some commands in each sub-folder

I need to write a script to work in Windows, that when executed will run a command in some of sub-directories, but unfortunately I have never done anything in batch, and I don't know where to start.
With the example structure of folders:
\root
\one
\two
\three
\four
I want the script to enter the specified folders (e.g. only 'one' and 'four') and then run some command inside every child directories of that folders.
If you could provide any help, maybe some basic tutorial or just names of the commands I will need, I would be very grateful.
You can tell the batch to iterate directories:
for /d %i in (C:\temp\*) do ( cd "%i" & *enter your command here* )
Use a percent sign when run directly on the command line, two when run from a batch
In a batch this would look something like this:
#echo off
set back=%cd%
for /d %%i in (C:\temp\*) do (
cd "%%i"
echo current directory:
cd
pause
)
cd %back%
Put the commands you need in the lines between ( and ).
If you replace C:\temp\ with %1 you can tell the batch to take the value of the directory from the first parameter when you call it.
Depending of the amount of directories you then either call the batch for each directory or read them from a list:
for /f %i in (paths.lst) do call yourbatch %i
The paths.lstwill look like this:
C:\
D:\
Y:\
C:\foo
All of this is written from memory, so you might need to add some quotations marks ;-)
Please note that this will only process the first level of directories, that means no child folders of a selected child folder.
You should take a look at this. The command you are looking for is FOR /R. Looks something like this:
FOR /R "C:\SomePath\" %%F IN (.) DO (
some command
)
I like answer of Marged that has been defined as BEST answer (I vote up), but this answer has a big inconvenience.
When DOS command between ( and ) contains some errors, the error message returned by DOS is not very explicit.
For information, this message is
) was unexpected at this time.
To avoid this situation, I propose the following solution :
#echo off
pushd .
for /d %%i in (.\WorkingTime\*.txt) do call :$DoSomething "%%i"
popd
pause
exit /B
::**************************************************
:$DoSomething
::**************************************************
echo current directory: %1
cd %1
echo current directory: %cd%
cd ..
exit /B
The FOR loop call $DoSomething "method" for each directory found passing DIR-NAME has a parameter. Caution: doublequote are passed to %1 parameter in $DoSomething method.
The exit /B command is used to indicate END of method and not END of script.
The result on my PC where I have 2 folders in c:\Temp folder is
D:\#Atos\Prestations>call test.bat
current directory: ".\New folder"
current directory: D:\#Atos\Prestations\New folder
current directory: ".\WorkingTime"
current directory: D:\#Atos\Prestations\WorkingTime
Press any key to continue . . .
Caution: in Margeds answer, usage of cd "%%i" is incorrect when folder is relative (folder with . or ..).
Why, because the script goto first folder and when it is in first folder it request to goto second folder FROM first folder !
On Windows 10 and later, it should be like this:
#echo off
for /D %%G in ("C:\MyFolderToLookIn\*") DO (
echo %%~nxG
)
This will show the name of each folder in "C:\MyFolderToLookIn". Double quotes are required.
If you want to show full path of the folder, change echo %%~nxG with echo %%G

Resources