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
Related
I am trying to write a batch script, which removes all non pdf files from a directory.
The directory name is OUTPUT/
this what i got
FOR /f %%f IN (.\OUTPUT) DO
SET fileName=%%f
IF NOT "!fileName:~-3!"=="pdf" (
DEL !fileName!
)
Although your question is simply that you're not using the correct syntax with your for command, there are other remarks I noted, which I decided would be better as an answer.
The most obvious one is that there is that you're setting a variable for no reason, and then having to use it with delayed expansion.
To do it as you were, it should look more like this:
#Echo Off
SetLocal DisableDelayedExpansion
For %%G In (Output\*) Do (
Set "fileName=%%G"
SetLocal EnableDelayedExpansion
If /I Not "!fileName:~-4!"==".pdf" (
Del /F "!fileName!"
)
EndLocal
)
These entire batch files should do the same thing.
Standard for loop:
#For %%G In (Output\*)Do #If /I Not "%~xG"==".pdf" Del /F "%%G"
Using the for /f variant:
#For /F "Delims=" %%G In ('Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"')Do #Del /F "Output\%%G"
or over multiple lines, if you find it easier to read:
#Echo Off
For /F "Delims=" %%G In (
'Dir /B/A-D-S-L "Output" 2^>NUL^|FindStr /VILE ".pdf"'
) Do (
Del /F "Output\%%G"
)
Please remember that almost every windows cli command has built-in help, and the vast majority accept the /? option, e.g. for /?, del /?. Additionally you can use the help command, e.g. help dir, help findstr.
I have a directory with multipe levels of folders.
I am completely new to writing batch files and I am writing my first one.
Stuck for ages on trying to
find all files in the directory including sub-folder
get parent directory for each file
save as variable like %parent.filename%
I have been searching here:
https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-xp/bb490909(v=technet.10)
And on Google but unfortunately I am stuck.
So now I managed to save the full path of each file as variable, but I want %Folder.FileName% to return the parent directory only, not the full path.
This is the code I have been testing in the command prompt.
For /F %A in ('Dir Linkedin /A-D /s /b /o') do SET Folder.%~nxA=%~pA
EDIT
I also saw this thread
And tried this code:
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
But %~nxB doesn't return any value... I expected it to get the last string of the path.
FOR /F %A in ('Dir Linkedin /A-D /s /b /o') do ECHO %~nxA %~pA. >>Paths.txt
FOR /F "tokens=1,2" %A in (Paths.txt) do SET Parent.%A=%~nB
Note the extra .
The path provided by the ~p modifier terminates in \ so adding . to this means "the directory name itself as though it was a filename"
As a one-line command (within a batch, decorated by standard palaver)
#ECHO OFF
SETLOCAL
FOR /F %%A in ('Dir test* /A-D /s /b /o') do FOR /F %%S in ("%%~pA.") do SET Parent.%%~nxA=%%~nS
set parent.
GOTO :EOF
I used the filemask test* to better suit my system.
I can't imagine you'd voluntarily perpetually re-type the command, so the format for use within a batch file is shown.
I would suggest you do this as a single nested For loop from the Command Prompt and with no output file:
For /F "Delims=" %A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do #For %B In ("%~pA.")Do #Set "Folder.%~nxA=%~nxB"
From a batch-file, perhaps this would help you out:
#Echo Off
Rem Remove any existing Folder. variables
For /F "Tokens=1*Delims==" %%A In ('Set Folder. 2^>NUL')Do Set "%%A="
Rem Set the new variables
For /F "Delims=" %%A In ('Dir /B/S/A-D "Linkedin" 2^>NUL')Do For %%B In ("%%~pA.")Do Set "Folder.%%~nxA=%%~nxB"
Rem View any returned variables
Set Folder. 2>NUL&&Pause
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
Looking for a way to run a command, but insert the path/filenames of all mp4 files from the working directory (where the script is run) to the command.
vlc.exe c:\path\filename1.mp4 c:\path\filename2.mp4
I have the following code, but the "%%~A" is only inserting one path/filename at a time instead of adding every path filename from the folder.
set dir=C:\Users\Administrator\Desktop\1
for /f "delims=" %%A in ('dir /b "%dir%\*.*"') do ("C:\Program Files (x86)\VideoLAN\VLC\vlc.exe" "%%~A" --sout=#transcode{vcodec=mp2v,vb=800,acodec=mpga,ab=128,channels=2,samplerate=44100}:http{mux=ts,dst=:8080/abc} --sout-keep --loop)
An elementary solution: first try
set "dir=C:\Users\Administrator\Desktop\1"
dir /b /a:-d "%dir%\*.*"
The same result (filenames only) as in:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%%~A"
So add full path as follows:
set "dir=C:\Users\Administrator\Desktop\1"
for /f "delims=" %%A in ('dir /b /a:-d "%dir%\*.*"') do #echo "%dir%\%%~A"
Eventually, replace #echo "%dir%\%%~A" with your command...
I want to create a 0 byte file names dblank in a specific directory C:\Users\myUser\*.data\.
echo. 2>"C:\Users\myUser\*.data\dblank.txt"
The * sign in the above command refers to any letters or numbers. I do not know. How can I refer to any letters or numbers in my batch code?
Maybe this:
setlocal enableextensions
for /D %%i in (C:\Users\myUsers\*.data) do copy nul "%%~i\dblank.txt"
endlocal
You can omit setlocal/endlocal if command extensions are already enabled (cmd /E:on).
This works on every existing *.data folder, if any.
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data') do echo. 2>"%%f\dblank.txt"
EDIT
Filter results:
#echo off
for /f "delims=" %%f in ('dir /b /s /ad C:\Users\myUser\*.data^|findstr /r "\\[0-9a-zA-Z]*\.data$"') do (
echo. 2>"%%f\dblank.txt"
)