I know that I can do basic globbing that prints me the full path like so: for %f in (pattern) do #echo %~dpnxf
However, if I do c:\dev>for %f in ("src/*.c") do #echo %~dpnxf, where with the pattern I'm searching within a subdirectory, instead of getting c:\dev\src\main.c I'm only getting c:\dev\main.c as a result. It basically doesn't include the any subdirectory in the p modifier.
I'm now stuck on that for longer than I'd like to admit... Is there a way to fix this?
cd /D %windir%\..&for %f in ("windows\exp*.exe") do #echo %~dpnxf works.
cd /D %windir%\..&for %f in ("windows/exp*.exe") do #echo %~dpnxf does not work.
I guess / is a problem. Just use the real path separator on Windows which is \.
Related
It's a bit of a convoluted title and I apologise for my poor English, it's not my first language and I'm far from fluent. I hope my current code explains my goal better than my written explanation.
#echo off
Setlocal enableextensions enabledelayedexpansion
set BCAT_PATH="C:\\Users\\USER\\Downloads\\FMOD conversion to packable\\0Tools\\bincat"
CD "9temp\\zzz_FSBS_Extract_test"
for /D %%D in (\*) do
"%BCAT_PATH%\\bincat" "%%D\*.ogg" -o "..\\zzz_BuiltOGG_test%%\~ni.tmp"
PAUSE
#echo off
Setlocal enableextensions enabledelayedexpansion
set "BCAT_PATH=C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat"
CD "9temp\zzz_FSBS_Extract_test"
for /D %%D in (*) do FOR %%i in ("%%D\*.ogg") do ECHO "%BCAT_PATH%\bincat" "%%i" -o "..\zzz_BuiltOGG_test\%%~ni.tmp"
PAUSE
Use set "var1=data" for setting string values - this avoids problems caused by trailing spaces. In comparisons; don't assign a terminal \, space or quotes - build pathnames from the elements - counterintuitively, it is likely to make the process easier.
Your CD statement will change to a directory RELATIVE to your current directory, so if you are currently at C:\somewhere to C:\somewhere\9temp\zzz_FSBS_Extract_test. If 9temp\zzz_FSBS_Extract_test is an absolute location, then you'd need \9temp\zzz_FSBS_Extract_test
for /D %%D in (\*) do would set %%D to each directoryname in the root directory. Since you've changed to ..?..9temp\zzz_FSBS_Extract_test, you need * to scan the current directory. You could also use "..?..9temp\zzz_FSBS_Extract_test\*" without changing directory. *I don't know where 9temp\... is, so I've used ..?.. to represent its location.
Note that the command to be executed must follow directly after the do, on the same physical line. I've added ECHO to show the command that would be executed. After you've verified that the command is correct, remove the echo keyword to actually execute the command.
Note that BCAT_PATH is set to C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat The command generated will thus be "C:\Users\USER\Downloads\FMOD conversion to packable\0Tools\bincat\bincat".
I've no idea where %%i is defined in your program. I've inserted it where I believe it should go. That should set %%i to each .ogg filename in the directory %%D in turn. %%~ni should return the name part of that file.
Your output directory would be ..?..9temp\zzz_BuiltOGG_test since your current directory is ..?..9temp\zzz_FSBS_Extract_test . The \ should be placed between the directoryname and the filename.
There is not need for delayedexpansion although setlocal is a good idea.
This will not do for each directory, but instead find each .ogg file recursively, then run the command on each file. Also note, I've added the .exe extension to bincat
#echo off
setlocal & set "BCAT_PATH=C:\Users\USER\Downloads\FMOD conversion to packable\\0Tools\bincat"
cd /d "9temp\\zzz_FSBS_Extract_test"
for /R %%i in (*.ogg) do "%BCAT_PATH%\bincat.exe" "%%~i" -o "..\zzz_BuiltOGG_test\%%~ni.tmp"
I have the below batchfile that output ReadFile.txt file
cd /d "T:\EMP\SST"
for /r %%i in (T:\EMP\SST) do echo %%i >>D:\MultiThreading\ReadFile.txt
pause
this ReadFile.txt file output
T:\EMP\SST\T:\EMP\SST
T:\EMP\SST\file 11\T:\EMP\SST
T:\EMP\SST\file 12\T:\EMP\SST
T:\EMP\SST\file 13\T:\EMP\SST
I want to remove the directory ouptput T:\EMP\SST so I want my output to be like this
file 11
file 12
file 13
how to do this
I believe you actually need for /D rather than for /R for your task (because I assume file 11, file 12, file 13 are actually directories as they are enumerated by for /R):
for /D %%I in (T:\EMP\SST\*) do echo %%~nxI
Anyway, your for /R syntax is wrong; the directory you want to enumerate recursively needs to be stated immediately after the /R switch (if you omit it, the current directory is assumed), and you need to provide a set (that is the part in between parentheses) that constitutes a pure file name pattern only, without any (absolute or relative) paths, like *.*, for example, to match all files.
In your code, for /R enumerates the current directory. Since your set is T:\EMP\SST and contains no wildcards (*, ?), it is just appended to the enumerated directories literally, because for accesses the file system only in case wildcards are encountered. That explains your weird output.
you're using FOR in a wrong way. The pattern between the parentheses isn't the start directory, it is the file/directory pattern you want to match. Here I suppose you want *.*
If you only want the filenames (no paths at all) you can write:
#echo off
for /r T:\EMP\SST %%i in (*.*) do echo %%~nxi>>D:\MultiThreading\ReadFile.txt
If you want the filenames + relative paths, it's slightly more complicated but doable, by enabling delayed expansion to be able to remove the path prefix + backslash:
#echo off
setlocal enabledelayedexpansion
set start_path=T:\EMP\SST
for /r %start_path% %%i in (*.*) do (
set f=%%i
echo !f:%start_path%\=!>>D:\MultiThreading\ReadFile.txt
)
This FOR command is cryptic and I have to read the help (FOR /?) everytime, but the fact is: everything useful is in there so I do it all the time :)
cd $PACKAGING_EARS_PATH/target/*/*
but in windows cd target/*/* is not working.
I want to get last directory. Is it possible?
See if this floats your boat:
#echo off
for /d /r "%PACKAGING_EARS_PATH%/target" %%a in (*) do set "folder=%%a"
cd "%folder%"
It looks like you are using Unix syntax to expand an environment variable. Windows uses %var%.
The foxidrive solution may not give the correct answer if there are deeper subdirectories.
A proper solution requires two steps.
From the command line:
for /d %A in ("%PACKAGING_EARS_PATH%\target\*") do #for /d %B in ("%A\*") do #cd %B
Using batch:
#echo off
for /d %%A in ("%PACKAGING_EARS_PATH%\target\*") do for /d %%B in ("%%A\*") do cd %%B
Windows cmd has an interactive command line TAB folder (and file) name completion feature that might be useful to you. For example, type cd te and then press the <tab> key and it will automatically expand to the first folder that starts with te. Repeatedly press <tab> and it will cycle through all matching folder names. You can omit leading characters and it will simply cycle through all folder names.
The TAB completion feature may or may not be enabled by default. CMD /F:ON will explicitly launch cmd with name completion enabled.
I have this command line for loop
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\*") do rmdir %f
It appears to be correct but it breaks up the path anyways even with the double quotes. I've tried using the hex equivalent (0x22) and that did not help either. Is there another way to handle paths with spaces in them?
If I simply try
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\New Folder") do rmdir %f
It keeps the whole string but as soon as I add the wildcard it breaks the path up. The overall goal is to delete all the folders in this folder without deleting the folder itself.
Note:These are just test folders while trying to solve this problem, the real folders could be named anything.
I usually solve this problem by using the old DOS 8 character path
"Documents and settings" is: "docume~1"
"Program files" is "progra~1"
"new folder" is newfol~1
If you have multiple similar names in a folder then the ~1 at the end is a counter
new folder = newfol~1
new folder2 = newfol~2
new folderhuppla = newfol~3
Double quote the rmdir argument. Also double the % when used in a batch file.
for /d %f in ("C:\Documents and Settings\Moi\Desktop\New Folder\*") do rmdir "%f"
I need to find the location of a specific directory, and then store that directory path into a variable within a Windows batch script.
I also want the command to return when it finds a match (to avoid searching the entire hard drive once the directory has already been found).
So far I've tried this on the command line:
dir c:\ /s /b /ad | find "DirectoryName"
The problem with this is that it searches the entire drive, even after a match is found. Plus, I still can't figure out how to store the result in a variable within a batch file. There should only be a single result.
Basically I need the equivilent of somehting like this on Linux/bash:
export DIRPATH=`find / -name "DirectoryName" -print -quit`
Thanks for looking!
In batch you need FOR /F to get the output of a command.
FOR /F "usebackq delims=" %%p IN (`dir c:\ /s /b /ad ^| find "DirectoryName"`) DO (
set "DIRPATH=%%p"
)
echo %DIRPATH%
As there are quotes in the find command you need the usebackq-option.
And it's necessary to escape the pipe character one time, as it should pipe the dir command, not the for command