This command does not work it wont accept spaces in the path name and I cant figure out how to fix it please help
for /f "tokens=* delims=" %%x in (E:\NON-school stuff\space space\a.txt) do echo %%x
I have tried everything please help!
Easy fix.
set "sourceFile=E:\NON-school stuff\space space\a.txt"
for /f "usebackq tokens=* delims=" %%x in ("%sourceFile%") do echo %%x
Anytime you have spaces in file paths you need to quote them and if you read the help for the FOR /F command you will see that the usebackq option allows you to use quotes when you have spaces in file names. If you don't use the usebackq option it treats the file name as a string.
one trick is to transform your filename into a short filename, so it may be accepted by any command without problems
for %%a in ("\some dir\some text.txt") do echo %%~sa
in your case
for %%a in ("E:\NON-school stuff\space space\a.txt") do for /f "tokens=* delims=" %%x in (%%~sa) do echo %%x
Related
I am trying to put the a .jar file path into a var... but It's killing me!
I have problems with spaces.... I tried all combinations possible with `'ยด" and no lucky...
Can't pass "%programfiles(x86)%\java" as parameter to "where" command :(
Obviously I'm not putting well the special characters, but not discover how to do it!
Also checked all options in:
Batch File: FOR /F doesn't work if path has spaces
for /f "usebackq" %%f in (''where /r '%programfiles(x86)%\java file.jar'') do set "jarpath=%%f"
for /f "usebackq" %%f in ("where /r `%programfiles(x86)%\java`" file.jar) do set "jarpath=%%f"
for /f "usebackq" %%f in (`where /r` "%programfiles(x86)%\java" file.jar) do set "jarpath=%%f"
I got it! if you need...
for /f "usebackq tokens=*" %%f in (`where /r "%programfiles(x86)%\java" file.jar`) do set jarfile=%%f
Please note: This question is not about how to echo without a newline. It's about to pipe a variable without a newline and store the result in another variable. Please don't mark this question as duplicate to other questions answering only how to remove newlines!
I have a variable a which I want to pass to a program (let's use more for the sake of this example), and its result should be stored into another variable b. This is to prevent the creation of temporary files. Sticking some answers from other questions here together, this could be achieved by something like this:
FOR /f "tokens=*" %%t IN ('ECHO %a% ^| MORE') DO SET b=%%t
Which works -- BUT will add another newline on my variable! So I tried the following tricks you find on stackoverflow/superuser to prevent the newline. If you do these without the loop, they work perfectly! But once you put them in a loop, they fail:
FOR /f "tokens=*" %%t IN ('ECHO ^| SET /p="%a%" ^| MORE') DO SET b=%%t
FOR /f "tokens=*" %%t IN ('^<NUL SET /p="%a%" ^| MORE') DO SET b=%%t
It will always say The syntax of the command is incorrect. What am I missing? Please help me!
If you run your command
FOR /f "delims=" %%t IN ('^<NUL SET /p="%a%" ^| more ') DO SET "b=%%t"
with echo ON you will see the equal sign in the set command has vanished, it has been removed by the parser leaving an incorrect command.
You have two options:
FOR /f "delims=" %%t IN ('^<NUL SET /p^="%a%" ^| more ') DO SET "b=%%t"
FOR /f "delims=" %%t IN ('^<NUL SET /p"=%a%" ^| more ') DO SET "b=%%t"
The first one escapes the equal sign. The second one quotes it.
I am working on a batch-script and trying to make it work in directories containing spaces. In a particular line I do the following loop:
for /f "tokens=*" %%A in ('%~dp0fciv\fciv.exe -md5 %~dp1%FN%') do ...
If the current directory contains spaces the loop will fail to call the executable. Now I put it double quotes to fix it:
for /f "tokens=*" %%A in ('"%~dp0fciv\fciv.exe" -md5 %~dp1%FN%') do ...
This works fine until the parameter doesn't have spaces. Thus I need to put it in the double quotes too:
for /f "tokens=*" %%A in ('"%~dp0fciv\fciv.exe" -md5 "%~dp1%FN%"') do ...
But this doesn't work as expected. I made additional tests straight in the cmd:
for /F "tokens=* usebackq" %A in (`"c:\Test Folder\fciv\fciv.exe" -md5 "d:\Somefile"`) do echo %A
for /F "tokens=*" %A in ('"c:\Test Folder\fciv\fciv.exe" -md5 "d:\Somefile"') do echo %A
The error is: "c:\Test" not recognized as an internal or external command, operable program or batch file.
I also tried to leave the second double quotes out again:
for /F "tokens=*" %A in ('"c:\Test Folder\fciv\fciv.exe" -md5 d:\Somefile') do echo %A
That command does surprisingly what it should.
Why does the error happening and how to achieve the desired functionality?
Enclose entire command, within the backquotes, in quotes.
C:\Users\User>for /F "usebackq tokens=*" %A in (`""C:\Users\Use r\Desktop\Editor\UEd\UEd.exe" -md5 "d:\Somefile""`) do echo %A
Removing the last backquote may have worked if no parameters.
I recently had this issue and the accepted answer did not work for me. I ended up wrapping the command and its functionality into another CMD file and then calling it from the FOR /F. Here is an example of the command:
wmic fsdir where name="C:\\some\\path\\to\\a\\folder" get creationdate
The path was extracted and passed in as a variable and the output captured and set in the DO section for the FOR /F of the calling script.
Hopes this helps someone in the future.
I have a txt file that contains the following lines
jfo3 93jfl
lvls 29fdj
nskd jfuwe
xlkw eklwe
I'm trying to read the file line by line, and do something with it. What delimiter should I use?
The delim I'm using here reads each word separately.
#echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%x in (lines.txt) do (
echo %%x
)
This reads line by line for me:
for /f "delims=" %x in (lines.txt) do echo %x
The problem is not related to delims, but to tokens:
for /f "tokens=*" %%x in (lines.txt) do echo %%x
If this is your input file:
abc,def
ghi,jkl
mno,pqr
then use
FOR /F "tokens=1,2,3 delims=," %%i in (test.txt) do (whatever u want)
How do I modify this:
for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
to work when the path contains spaces?
For example, if this is run from
c:\my folder with spaces
it will echo:
c:\my
Thanks
You need to use:
for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
This overrides the default delimiters which are TAB and SPACE
I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause
FOR /F %%A IN ('type "c:\A Path With Spaces\A File.txt"') DO (
ECHO %%A
)
This article gave me the idea to use "type" in the IN clause.
If you don't want to deal with "quotes" you can use the "s" switch in %~dpnx[]...
this will output the short filenames that are easy to work with.
from the Command line...
for /f "delims=" %f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %~sdpnxf
inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]
for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf
The problem is there's a wildcard in the string that gets interpreted as a filename. You also need double quotes for a string with spaces. I'm not sure if there's a way to escape the wildcard.
for %a IN ("dir /b /s build\release\.dll") do echo %a
"dir /b /s build\release\.dll"