Change to directory using variable name - windows

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%

Related

Windows command FOR in batch file, specified to work in a certain folder

Normally I can specify a folder for a batch file to work in.
Not so with the FOR command:
for %%a in (G_*.txt) do ren "%%a" "test-%%a"
This finds G_*.txt in all files and renames those by putting test- in front of the filename.
I tried specifying G_*.txt further with C:\test\G_*.txt but that is not accepted.
I also tried pouring this into a variable but that also failed.
Who knows what to do?
Again, I had to change the approach: using SET /R will put a long path into the %%a variable which makes the idea unsuited to put a bit of text in front of a filename.
I found the solution by selecting the work directory first and let the FOR construction do its work. Since it must be network proof, I had to use the PUSHD command.
This is what the final result looks like:
set source=\\nassie\home\test\source with nasty space
set target=D:\target
PUSHD %source%
:: If this fails then exit
If %errorlevel% NEQ 0 goto:eof
for %%a in (G_*.txt) do xcopy "%source%\%%a" "%target%\textblabla_%%a*" /D /Y
POPD

A bat file to recursively traverse and find certain file type

I am trying to recursively traverse through a directory and find exe files in it.
This is the folder structure
C:\MyCave\iso\SDG\cmpdir
---------------\FolderA
------------------abc.txt
---------------\FolderB
------------------def.exe
---------------\FolderC
------------------ghi.dll
The below is my bat sinppet.
set f="C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r %%f in ("*.exe") do if exist %%f echo %%f
Though it is working and listing out exe files in C:\MyCave\iso\SDG\cmpdir, but I want to list exe files present only in C:\MyCave\iso\SDG\cmpdir\test-recur directory.
This is my current output.
C:\MyCave\iso\SDG\cmpdir>exeRecur.bat
C:\MyCave\iso\SDG\cmpdir>set f="C:\MyCave\iso\SDG\cmpdir\test-recur"
C:\MyCave\iso\SDG\cmpdir>for /R %f in ("*.exe") do if exist %f echo %f
C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\cp.exe echo C:\MyCave\iso\SDG\cmpdir\cp.exe
C:\MyCave\iso\SDG\cmpdir\cp.exe
C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe echo C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe
C:\MyCave\iso\SDG\cmpdir\dirB\Abcd.exe
C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe echo C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe
C:\MyCave\iso\SDG\cmpdir\test-recur\2\def.exe
C:\MyCave\iso\SDG\cmpdir>if exist C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe echo C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe
C:\MyCave\iso\SDG\cmpdir\wspace\defg.exe
I know there is a small mistake somewhere. Requesting your help in solving it.
Based on Gerhard's and sst's answer I have modified my script like this.
set rootFolder=%1
set destFolder=%2
for /r %rootFolder% %%f in ("*.exe") do if exist %%f move %%f %destFolder%
I am passing command line arguments to bat like this...
exeRecur.bat "C:\MyCave\iso\SDG\cmpdir\test-recur" "C:\MyCave\iso\SDG\cmpdir\dirA"
Like this hard-coding of paths is avoided.
Regards
Ok, so you are setting a variable:
set f="C:\MyCave\iso\SDG\cmpdir\test-recur"
Firstly, it is bad practice to set single char variables, but more importantly is the way you wrap it with double quotes. It should be from the beginning of the variable to the end of the value.
set "f=C:\MyCave\iso\SDG\cmpdir\test-recur"
The real problem though is that you never tell the for /r loop where to recursively do the search.
set "mypath=C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r "%mypath%" %%f in (*.exe) do echo %%~f
You forgot to specify the 'root path' argument of FOR /R, So it begins the enumeration from the current directory.
You should use a different name for your environment variable to avoid confusion with the FOR's parameter %%f.
set "RootFolder=C:\MyCave\iso\SDG\cmpdir\test-recur"
for /r "%RootFolder%" %%f in (*.exe) do echo %%f
You should also use the extended syntax of the set command: set "var=value" and use "%var%" wherever quoting is needed.
Since you are using the wildcard *.exe, There is no need for if exist in FOR /R as it will only enumerate the existing files.

Select a directory by checking its name for date and time in Windows Batch

i have a batch file scripted which makes a backup of some of my mysql database tables and saves them in a new created folder with this pattern as name: "date_time" so as example "23.06.2016_1050".
So i have another batch file which imports every *.sql file in a specific folder back to my database.
What i want to do is, writing a batch which automaticly selects the latest folder and then runs my part of code in that.
Here is my batch code:
cd c:\server\backup\character_data
FOR %%X IN (*.sql) DO ECHO Importing %%X & "C:\Program Files\MariaDB 10.1\bin\mysql" dspdb -h localhost -u root -p 123456789 < %%X
So i need to cd a %variable% which contains the name of the folder with the latest date and time pattern as its name.
So this:
cd c:\server\backup\character_data\%variable%
would be:
cd c:\server\backup\character_data\23.06.2016_1050
Does anyone knows how to get the desired folder name as variable somehow?
I really hope that i explained it good enough and someone can help me with that.
Thx in advance! :)
This will get the most recent folder in c:\server\backup\character_data with the full file path, so no need to do cd c:\server\backup\character_data\%variable%, instead it would be cd %a%
If you don't want the full directory of the folder then remove the /s from the dir command
#echo off
cd /d "c:\server\backup\character_data"
FOR /F "delims=" %%i IN ('dir /b /s /ad-h /t:c /od') DO SET a=%%i
::Do what you want with your folder, call it with %a%

How to execute an application existing in each specific folder of a directory tree on a file in same folder?

I have some folders with different names. Each folder has a specific structure as listed below:
Folder1
Contents
x64
Folder1.aaxplugin
TransVST_Fixer.exe
Folder 2
Contents
x64
Folder 2.aaxplugin
TransVST_Fixer.exe
There are two files within each subfolder x64. One file has the same name as the folder two folder levels above. The other file is an .exe file whose name is the same in all folders.
Now I need to run file with file extension aaxplugin on each specific .exe file. It would be obviously very time consuming opening each and every single folder and drag & drop each file on .exe to run it on this file.
That's why I am trying to create a batch script to save some time.
I looked for solutions here on Stack Overflow. The only thing I have found so far was a user saying this: When I perform a drag & drop, the process 'fileprocessor.exe' is executed. When I try to launch this exe, though, CMD returns error ('not recognized or not batch file' stuff).
How can I do this?
UPDATE 12/22/2015
I used first a batch file with following line to copy the executable into x64 subfolder of Folder1.
for /d %%a in ("C:\Users\Davide\Desktop\test\Folder1\*") do ( copy "C:\Program Files\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%a\x64\" 2> nul )
After asking here, I tried the following script:
for /f "delims=" %%F in ('dir /b /s x64\*.aaxplugin') do "%%~dpFTransVST_Fixer.exe" "%%F"
Unfortunately, the output is as following
C:\Users\Davide\Desktop>for /F "delims=" %F in ('dir /b /s x64\*.aaxplugin') do "%~dpFTransVST_Fixer.exe" "%F"
The system cannot find the file specified.
Try the following batch code:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\Desktop\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
The command FOR with option/R searches recursive in all directories of directory %USERPROFILE%\Desktop\test being expanded on your machine to C:\Users\Davide\Desktop for files with file extension aaxplugin. The loop variable F contains on each loop run the name of the found file with full path without surrounding double quotes.
The drive and path of each found file is assigned to environment variable FilePath.
Next a case-sensitive string comparison is done between file path with all occurrences of string \x64\ case-insensitive removed with unmodified file path.
Referencing value of environment variable FilePath must be done here using delayed expansion because being defined and evaluated within a block defined with ( ... ). Otherwise command processor would expand %FilePath% already on parsing the entire block resulting in a syntax error on execution because string substitution is not possible as no environment variable FilePath defined above body block of FOR loop.
The strings are not equal if path of file contains a folder with name x64. This means on provided folder structure that the file is in folder x64 and not somewhere else and therefore the application is executed next from its original location to fix the found *.aaxplugin file.
The line with IF is for the folder structure example:
if not "C:\Users\Davide\Desktop\test\Folder1\Contents" == "C:\Users\Davide\Desktop\test\Folder1\Contents\x64\"
if not "C:\Users\Davide\Desktop\test\Folder 2\Contents" == "C:\Users\Davide\Desktop\test\Folder 2\Contents\x64\"
So for both *.aaxplugin files the condition is true because the compared strings are not identical
Also possible would be:
#echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%F in ('dir /A-D /B /S "%USERPROFILE%\test\*.aaxplugin" 2^>nul') do (
set "FilePath=%%~dpF"
if not "!FilePath:\x64\=!" == "!FilePath!" "%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%F"
)
endlocal
But command DIR is not really necessary as it can be seen on first provided code.
But if the application TransVST_Fixer.exe for some unknown reason does its job right only with directory of file being also the current directory, the following batch code could be used instead of first code using the commands pushd and popd:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%F in (*.aaxplugin) do (
set "FilePath=%%~dpF"
echo !FilePath!
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dpF"
"%ProgramFiles%\Sugar Bytes\TransVST\TransVST_Fixer.exe" "%%~nxF"
popd
)
)
endlocal
There is one more difference in comparison to first code. Now the last 5 characters of path of file are compared case-insensitive with the string \x64\. Therefore the file must be really inside a folder with name x64 or X64. A folder with name x64 or X64 anywhere else in path of file does not result anymore in a true state for the condition as in first two batch codes.
But if for some unknown reason it is really necessary to run the application in same folder as the found *.aaxplugin and the directory of the file must be the current directory, the following batch code could be used:
#echo off
setlocal EnableDelayedExpansion
for /R "%USERPROFILE%\test" %%# in (*.aaxplugin) do (
set "FilePath=%%~dp#"
if /I "!FilePath:~-5!" == "\x64\" (
pushd "%%~dp#"
"%%~dp#TransVST_Fixer.exe" "%%~nx#"
popd
)
)
endlocal
The path of the file referenced with %%~dpF always ends with a backslash which is the reason why there is no backslash left of TransVST_Fixer.exe (although command processor could handle also file with with two backslashes in path).
In batch code above character # is used as loop variable because %%~dp#TransVST_Fixer.exe is easier to read in comparison to %%~dpFTransVST_Fixer.exe. It is more clear for a human with using # as loop variable where the reference to loop variable ends and where name of application begins. For the command processor it would not make a difference if loop variable is # or upper case F.
A lower case f would work here also as loop variable, but is in general problematic as explained on Modify variable within loop of batch script.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
dir /?
echo /?
endlocal /?
for /?
if /?
popd /?
pushd /?
set /?
setlocal /?
Your question isn't quite clear, but it seems, something like this should work:
for /f "delims=" %%f in ('dir /b /s X64\*.ext') do "%%~dpfMyExe.exe" "%%f"
Maybe you have to change directory to each folder (depends on your .exe):
for /f "delims=" %%d in ('dir /B /ad') do (
pushd "%%d"
for /f "delims=" %%f in ('dir /b "contents\x64\*.ext"') do (
cd Contents\x64
MyExe.exe "%%f"
)
popd
)
Assuming:
The Directory structure is fixed and the files are indeed in a subfolder contents\X64\.
MyExe.exe is the same (name) in every folder.
There is only one file *.ext in every folder.
I'll give you the script I created for doing so, hope it works for you
for /d %%d IN (./*) do (cd "%%d/Contents/x64" & "../../../TransVST_Fixer.exe" "%%d" & cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins")
Please note that I placed the fixer inside the root folder so I just have to copy it once. You have to place it inside your root folder and execute it. What it does:
iterate over each folder
for each one it enters /Contents/x64, executes the fixer (wich is 3 levels above) and after that returns to the original folder.
If you have your plugins in a different folder, you just have to change this part replacing the path for the one you have your plugins in.
cd "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins"
REMEMBER to place the script on that folder. For this example I place my script on the folder "/Program Files (x86)\Common Files\Avid\Audio\Plug-Ins" and run it (as a .bat).
PS: the fixer will place the fixed plugins in "C:\Users\Public\modified" (just read the screen while executing, it gives you the new files path. If you want to move them to the right path, you can execute this from the new files path ("C:\Users\Public\modified")
for %%d IN (*.aaxplugin) do (mkdir "%%d_temp/Contents\x64" & move "%%d" "%%d_temp/Contents\x64/%%d" & rename "%%d_temp" "%%d")
with that, I iterate over every plugin and create a folder with the same name (I create _temp because of name colision, after moving the file I rename it to the correct one), also with the subfolder "/Contents/x64", and move the plugin inside. Once donde, you can just take the resulting folders and place them in their correct path.
Hope it works, for me it works like a charm.

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