By using:
dir /s /b /o:n /a:d > foldername.txt
I get the following output:
D:\Project\Java\MyName
D:\Project\Java\Object
But I want the output to look like this:
MyName
Object
The output have to be folder names without their paths?
The FOR loop has variable modifiers such that only the file name and extension can be presented. Note that a directory can have an extension. Use FOR /? for information about the variable settings.
FOR /F "usebackq tokens=*" %d IN (`DIR /S /B /A:D /O:N`) DO (ECHO "%~nxd")
Or, to put the names into a file without quoting:
DEL foldername.txt
FOR /F "usebackq tokens=*" %d IN (`DIR /S /B /A:D /O:N`) DO (ECHO>>foldername.txt %~nxd)
just use
DIR D:\Project\Java\ /b
the result will be just like what you want
Related
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
When i try to run this program it does not set the variable right. Is there anyway so that after it is done to set the file location not the file itself to a variable and have it printed on screen.
#echo off
for %%a in (d) do if exist "%%a:\" dir /b /s /a-d "%%a:\gm_construct.bsp" set p=%%~dpnxa
pause
What your code does:
dir /b /s /a-d "%%a:\gm_construct.bsp" set p=%%~dpnxa
lists all files "%%a:\gm_construct.bsp" and all files named set and all files named p=%%~dpnxa
What (I think) you want to do:
dir /b /s /a-d "%%a:\gm_construct.bsp"
and set it's output to the variable %p%
To get a command's output, you need another for:
for /f "delims=" %%i in ('dir /b /s /a-d "%%a:\gm_construct.bsp"') do set p=%%~dpnxi
integrated into your code (note the kind of the single quotes: '):
#echo off
for %%a in (d) do (
if exist "%%a:\" (
for /f "delims=" %%i in ('dir /b /s /a-d "%%a:\gm_construct.bsp"') do set p=%%~dpnxi
)
)
pause
pause
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 am currently running the command dir /b/s *.* on a specific directory that has subdirectories. How do I only get a list of directories that does not show empty directories, but only directories that have files.
Assuming the following structure (and this would be the result of dir /s /b)
test\1
test\2
test\3
test\1\fred.txt
test\2\wilma.txt
For a list of directories containing at least one file:
for /f %i in ('dir /b /s /ad') do #for /f %j in ('dir %i ^| find "File(s)"') do #if not %j==0 echo %i
Results:
test\1
test\2
For a list of files but not including lines that are just the directories:
for /f %i in ('dir /b /s /ad') do #for /f %j in ('dir %i ^| find "File(s)"') do #if not %j==0 dir /b /s %i
Results:
test\1\fred.txt
test\2\wilma.txt
Remember that if this is done inside of a batch file you have to double up the % signs, e.g. %%i and %%j
This will give you a list of folders without files:
#echo off
(for /f "delims=" %%a in ('dir /s /b /ad') do dir "%%a" /b /a-d >nul 2>&1 && echo %%a)>"folderlist.txt"
I am looking how to get list of all directories to be used in FOR loop.
So far I have work around:
set folderList = (folder1 folder2 folder3 folder4)
FOR %%i in %folderList% do zip %%i D:\...my_path...\%%i\*.*
is it possible that folderList would be generated dynamically ?
assuming you want to list subdirectories of c:\temp
for /f %%i in ('dir c:\temp /ad /b') do echo %%i
this will list foldernames of c:\temp, if you want get it recursively just add /s to dir command:
for /f %%i in ('dir c:\temp /ad /b /s') do echo %%i
as for #dbenham comment (thank you) to correctly handle dirs with space just add tokens=* to for :
for /f "tokens=*" %%i in ('dir c:\temp /ad /b') do echo %%i
Please try below code:
for /d %%F in ("d:\...my_path...\*") do zip "%%~nxF" "%%F\*.*"
I am not sure what is different but the double %% listed above are all failing to work.
This, however, works for me:
for /f "tokens=*" %i in ('dir c:\temp /ad /b') do echo %I