This question might have a simple solution, but I just cannot find it.
Let's say I'm using the DIR command to produce a list of .txt files in the folder "E:\Documents". How can I make sure that only the .txt files one level below "E:\Documents" are listed (i.e. in a direct subfolder) and not the files in "E:\Documents" itself. "E:\Documents\\" or "E:\Documents\*\" doesn't seem to do the trick.
Thanks in advance!
FOR /f "delims=" %%i IN ('dir /b/ad') DO IF EXIST ".\%%i\*.txt" DIR ".\%%i\*.txt"
That's if you want the output in DIR format - with headers
FOR /f "delims=" %%i IN ('dir /b /ad') DO IF EXIST ".\%%i\*.txt" (
FOR /f "delims=" %%q IN ('DIR /b ".\%%i\*.txt" ') DO ECHO ".\%%i\%%q"
)
if you want just the filenames.
This can be achieved without dir:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~ff
If you need not only the file names, but also detail information about the files, add the respective qualifiers, e.g.:
for /d %%d in ("C:\basedir\*") do for %%f in ("%%~fd\*.txt") do echo %%~azff
a → attributes of the file
t → timestamp of the file
z → size of the file
See help for for details.
Related
I have a series of files that I download/process regularly and need to use a batch file to rename. Each filename is something like word-word-word-datetime.csv. There is always a '-' between words and always -datetime before the '.csv' file extension. I need to remove the -datetime so that the files are named word-word-word.csv. In some cases there might be just one word before the -datetime but there can be a string of many words as well. I download these files and move them to a specific folder for processing, and there is already a batch file in the folder that I need to modify to also rename the files.
For example, I need the filenames below:
this-is-a-file-20200804134809.csv
another-file-20200804134750.csv
some-other-file-20200804134699.csv
file-20200804134389.csv
To be renamed to:
this-is-a-file.csv
another-file.csv
some-other-file.csv
file.csv
This answer is almost exactly what I need, but I'm not familiar enough with the syntax to modify it for renaming files with multiple hyphenated words (code from linked answer copied below).
#echo off
for /F "tokens=1,* delims=-" %%a in ('dir /A-D /B "*.mp4"') do (
echo move "%%a-%%b" "%%a%%~xb"
)
I was able to rename all of the .csv files in my folder, truncating the name to remove the last 15 characters as suggested by #Compo.
#echo off
setlocal enabledelayedexpansion
for %%f in (*.csv) do if %%f neq %~nx0 (
set "filename=%%~nf"
ren "%%f" "!filename:~0,-15!%%~xf"
)
Here's a more robust example, using the advice I provided in the comments:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "EOL=| Delims=" %%G In ('Dir /B /A-D *-??????????????.csv ^
^|"%__AppDir__%findstr.exe" /IR ^
"\-19[0123456789]*\.csv$ \-20[0123456789]*\.csv$"') Do (
Set "BaseName=%%~nG"
SetLocal EnableDelayedExpansion
Ren "%%G" "!BaseName:~,-15!%%~xG"
EndLocal
)
I am an absolute newbie to batch programming. I have posted this question after some amount of searching. Kindly guide me.
I have a folder containing a thousand images:
000001_x_abc1.jpg
000001_x_efg1.jpg
000001_x_hij1.jpg
000002_x_abc1.jpg
000002_x_efg1.jpg
000002_x_hij1.jpg
.
.
.
.
234562_x_abc2.jpg
234562_x_efg2.jpg
234562_x_hij2.jpg
Of theses files I have generated a 'list of files' that I need to pull out based on partial names i.e the numeric ID - first 6 numeric values in the file name e.g 234562*.jpg and copy them to a destination folder.
Note: Every numeric ID based search should give me 3 files and I need to copy all three. Any help would be appreciated.
I have tried the following code based on my search:
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "DEST_DIR=my_desination"
SET "SEARCH_DIR=my_source"
FOR /F "tokens=*" %%a IN (%~dp0my_list.txt%) DO (
FOR /R "%SEARCH_DIR%" %%f IN (*%%a*) DO (
SET "SRC=%%~dpf"
SET DEST=!SRC:%SEARCH_DIR%=%DEST_DIR%!
xcopy /S /I "%%~f" "!DEST!"
)
)
And my list file is as below:
002631_*.jpg
054741_*.jpg
054992_*.jpg
055053_*.jpg
055054_*.jpg
055118_*.jpg
055267_*.jpg
055294_*.jpg
055382_*.jpg
055415_*.jpg
055466_*.jpg
055546_*.jpg
This is an example of copying specific files to a folder.
#ECHO OFF
set "SOURCE_DIR=%userprofile%\Desktop\Source"
set "DEST_DIR=%userprofile%\Desktop\Output"
set "FILENAMES_TO_COPY=test.txt test1.txt test2.txt"
pushd "%SOURCE_DIR%"
for %%F IN (%FILENAMES_TO_COPY%) do (
echo file "%%F"
xcopy /Y "%%F" "%DEST_DIR%\"
)
popd
pause
The script copies test.txt test1.txt and test2.txt from the folder Source, to the folder Output
Check out this page
Supposing the list file contains full file patterns one per line, the following should work for you:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
rem Define constants here:
set "SOURCEDIR=D:\Data"
set "TARGETDIR=D:\BackUp"
set "LISTFILE=D:\files.lst"
cd /D "%SOURCEDIR%" || exit /B 1
for /F "usebackq delims= eol=|" %%L in ("%LISTFILE%") do (
for /F "delims= eol=|" %%F in ('dir /B "%%L"') do (
copy "%%~F" "%TARGETDIR%\%%~nxF"
)
)
endlocal
exit /B
(edited to reflect additional information in the question)
for /f %%a in (partial.txt) do copy %%a "x:\destination folder\"
for every entry in the textfile copy <entry from textfile> to the destination.
see for /? for more details
I want to generate a file that contains the directory structure of a given directory, as relative paths. Currently I have the following batch file:
#echo off
pushd "C:\TEST"
dir /AD /B /ON /S
popd
Its current output is this:
C:\TEST\one
C:\TEST\three
C:\TEST\two
C:\TEST\one\a
C:\TEST\three\d
C:\TEST\three\e
C:\TEST\three\f
C:\TEST\two\b
C:\TEST\two\c
I want the output to be:
one
three
two
one\a
three\d
three\e
three\f
two\b
two\c
EDIT: this question is not a duplicate. The other question shows files exclusively, now I need to get directories exclusively (not mixed with files).
#echo OFF
SETLOCAL enabledelayedexpansion
SET "targetdir=c:\106x"
pushd "%targetdir%"
FOR /f "delims=" %%a IN ('dir /AD /B /ON /S') DO (SET "dirname=%%a"&ECHO(!dirname:%targetdir%\=!)
popd
GOTO :EOF
This should work for you provided the directoryname does not contain !
The directorynames are applied to %%a and transferred to dirname for manipulation. The target directoryname + a closing \ are then replaced by nothing for display.
I have a directory with a list of files, I would like that my batch finds the most recently added file and then copies the content in another file in another directory. So far I did like this:
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do type %%X >> c\dir2\all.log
The problem is that in this way it outputs for example
ex150422.log 1>>c:\dir2.log
but it doesn't copy the content..
What am I doing wrong?
Thanks!
are you shure it can find the file? add the directory name like you did in dir-command
for /f "delims=" %%x in ('dir /od /a-d /b c:\dir1\*.log') do (
type c:\dir1\%%x>>c:\dir2\all.log
)
I want to delete all the files in the current directory which do not contain the string "sample" in their name.
for instance,
test_final_1.exe
test_initial_1.exe
test_sample_1.exe
test_sample_2.exe
I want to delete all the files other than the ones containing sample in their name.
for %i in (*.*) do if not %i == "*sample*" del /f /q %i
Is the use of wild card character in the if condition allowed?
Does, (*.*) represent the current directory?
Thanks.
Easiest to use FIND or FINDSTR with /V option to look for names that don't contain a string, and /I option for case insenstive search. Switch to FOR /F and pipe results of DIR to FIND.
for /f "eol=: delims=" %F in ('dir /b /a-d * ^| find /v /i "sample"') do del "%F"
change %F to %%F if used in a batch file.
The answer from Aacini worked for me. I needed a bat file to parse the directory tree finding all files with xyz file extension and not containing badvalue anywhere in the path. The solution was:
setlocal enableDelayedExpansion
for /r %%f in (*.xyz) do (
set "str1=%%f"
if "!str1!" == "!str1:badvalue=!" (
echo Found file with xyz extension and without badvalue in path
)
)
setlocal EnableDelayedExpansion
for %i in (*.*) do (set "name=%i" & if "!name!" == "!name:sample=!" del /f /q %i)