I have a batch file that is appending a date to all to the file name of all CSV files. I only want CSV files in one directory to be picked up and no subdirectories. It appears to be running through all subdirectories though.
I have this code currently in the batch file
:: copy files
For /f "delims=" %%a in ('Dir /A:-D /b /s "%LOCALDIR%"*.csv 2^>nul') do If exist "%%a" (
COPY "%%a" "%LOCALDIR%%dtt%-%%~na.csv"
DEL "%%a"
)
I have tried getting rid of the /s in the code but then no files are picked up in the directory I want to look for.
Any help is greatly appreciated.
Why not just use a simple loop like?
pushd %LOCALDIR%
for %%A in (*.csv) do ren "%%~A" "%dtt%-%%~A"
popd
Or for a one liner
for %%A in (%LOCALDIR%\*.csv) do ren "%%~A" "%dtt%-%%~A"
If the path has spaces in it remember to use the usebackq option.
Related
I have a bunch of folders and inside each of those folders are multiple files (all mkv files if that matters)
What I would like to do is run through each of the folders and replace every mkv file within the folder with an empty text file named after the deleted mkv file.
The mkv files all have different names. Can this be done with a batch file and copy /b NUL EmptyFile.txt somehow?
You can simply loop through the directories and use the variable expansion to get the name, create the text file and then delete the .mkv file:
#echo off
for /F "delims=" %%i in ('dir /b /s *.mkv') do break>"%%~dpni.txt" && del /Q /S "%%~i"
This will simply assign each file name to the metavariable %%i then expand to the drive, path and name %%~dpni and add an extension, in your case you wanted .txt using break to create an empty file.
Please first test this in a test folder before you run it on your actual files.
FOR /F "usebackq delims=" %%A IN (`DIR /A:d /B "C:\ParentFolder"`) DO (
FOR /F "usebackq delims=" %%B IN (`DIR /B "C:\ParentFolder\%%A"`) DO (
DEL /Q /F "C:\ParentFolder\%%A\%%B" > NUL 2>&1
ECHO. > "C:\ParentFolder\%%A\%%~nB.txt"
)
)
This will search for all the folders within C:\ParentFolder, and then fetch all files within the subfolders, and then delete any found files and echo (print) a blank line to a txt file with the same name as the original file.
DIR lists all files within a given folder, /B specifies that only bare file names and extensions are to be output. /A:d specifies to list directories (folders) only.
DEL deletes files, /Q specifies quietly with no confirmation, /F forces deletion of read only files (Not really necessary in this case but whatever)
ECHO. echoes (prints) a blank line, > outputs it to the provided file (It will create the file if it doesn't exist). %%~nB specifies only the name of the file, excluding the extension.
If you are not using this in a script, the following code works as a one-liner: FOR /F "usebackq delims=" %A IN (`DIR /A:d /B "C:\ParentFolder"`) DO ( FOR /F "usebackq delims=" %B IN (`DIR /B "C:\ParentFolder\%A"`) DO DEL /Q /F "C:\ParentFolder\%A\%B">NUL 2>&1 & ECHO. > "C:\ParentFolder\%A\%~nB.txt" )
I've previously asked the following question and was helped with a great solution.
How to compress all files with exception of newest file in each subfolder of a folder into one ZIP file per subfolder?
Now at the end of the year I would like to modify this solution so that it zips any remaining files from last year into the existing zip file.
The directory structure is like this:
Directory-Parent
---Sub-Directory-1
--------File1
--------File2
--------File3
---Sub-Directory-2
--------File1
--------File2
As mentioned and referred to in my previous question, I have a batch at the Directory-Parent level that will create a zip in each sub-directory of all the files except the latest file (or amount I specify). It also adds the year to the end of the zip file name.
So the result is:
Directory-Parent
-Sub-Directory-1
--------File1
--------Sub-Directory-12019.zip
---Sub-Directory-2
--------File1
--------Sub-Directory-22019.zip
The following is the batch file solution I have from my previous question. It zips all the files in each sub-directory (ignoring folders that I specify) into a zip file with the name of the directory appended by the current year.
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "FilesToIgnore=2"
set "theYear=%DATE:~-4%"
set "ListFile=%Temp%\%~n0.lst"
set "Move2=-sdel" & rem // (set this to `-sdel` if you want the files to be moved into the archive)
set "FoldersToExclude=FoldersToExclude.txt"
del "%ListFile%" 2>nul
REM for /D %%I in (*) do call :CompressFiles "%%I"
for /F "eol=| delims=" %%I in ('dir /AD-H /B /ON 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /R /V /X /G:"%FoldersToExclude%"') do call :CompressFiles "%%I"
goto EndBatch
:CompressFiles
pushd %1
set "ZipFile=%~nx1%theYear%.zip"
for /F "skip=%FilesToIgnore% eol=| delims=" %%J in ('dir /A-D /B /O-D /TW 2^>nul ^| %SystemRoot%\System32\findstr.exe /I /L /V /X /C:"%ZipFile%"') do >>"%ListFile%" echo %%J
if exist "%ListFile%" (
echo Compressing files in directory %1 ...
echo 7z a %Move2% -bso0 -i"#%ListFile%" -mx5 -scsDOS "%ZipFile%"
7z a %Move2% -bso0 -i"#%ListFile%" -mx5 -scsDOS "%ZipFile%"
del "%ListFile%"
)
popd
goto :EOF
:EndBatch
pause
endlocal
I cannot figure out how to modify this to instead zip all files with a specified (or previous) year.
I believe the answer is in the second for loop in the dir command. Is there a way in the dir command to filter by date.
Is there a way to use forfiles command instead with #fdate?
Thank you for your help.
I have directories like:
c:\Project\Current\stage1\somefiles and some folders
c:\Project\Current\stage2\somefiles and some folders
c:\Project\Current\stage3\somefiles and some folders
c:\Project\Current\stage4\somefiles and some folders
c:\Project\Current\stage5\somefiles and some folders
.
.
.
c:\Project\Current\stage500\somefiles and some folders
I want to create a batch file so that everything inside stage1, stage2,..., stage500 will get deleted but not any of other folders so that I can still see the above directories but empty.
Can someone please help?
Try this:
#echo off
CD c:\Project\Current /d
for /f "tokens=*" %%f in ('dir /a-d /s /b') do (
del "%%f" /q /f
)
There are three important parts:
for /f "tokens=*" %%f means we are iterating over all lines that are generated by the following command and temporarily save each line in the variable %%f for each iteration.
dir /a-d /s /b is the core of the code. This will list all files inside c:\Project\Current\ including all subfolders. /a-d means that directories will be ignored as we don't want them to be erased. /s means we are searching any subfolder. /b sets the output format to simple mode so that each line of the output will contain nothing but the full path to a file.
del "%%f" /q /f simply deletes the file which is stored in %%f. /q means "don't ask me if I'm sure, just erase it" and /f means that any file - even if it is marked as system file or as invisible or protected - will be deleted. Don't miss the quotation marks around %%f as otherwise paths containing spaces will cause trouble.
I found the answer and is very simple
for /d %%X in (c:\Project\Current*) Do (
for /D %%I in ("%%X\*") do rmdir /s/q "%%I"
del /F /q "%%X\*")
Thanks for everyone's help..
I have some files that I need to rename and as the volume is quite big, I was thinking of using a batch file since all I need to do is:
u_ex140429.log >> u_ex140429_01.log
But all the codes I found either place the _01 at the beginning or at the end of the file:
_01u_ex140429.log or u_ex140429.log_01
If there is a thread somewhere here about this, I do apologize for I have been looking for quite a while.
Thanks for your help guys.
This adds _01 before the extension of each .txt filename in the current folder without the limitation of a plain for-in-do command.
#echo off
for /f "delims=" %%a in ('dir *.txt /b /a-d ') do ren "%%a" "%%~na_01%%~xa"
Try using this.
#echo off
for %%f in (*.*) do (move %%f %%~nf_01%%~xf )
technet
a for loop works just fine ...
#echo off
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
for /L %%n in (1 1 %%f) do (
for %%a in (*.*) do (
rename "%%a" "%%~na_%%n%%~xa"
)
)
)
place the batch file in the directory containing your files to be renamed
I'm writing a batch file to cleanup my source folders. I want to delete all wincvs generated files with a prefix of .#
FOR /F "tokens=*" %%G IN ('DIR /B .#*.*') DO DEL "%%G"
the problem I'm having is that it's not deleting my files within subdirectories.
I think you need DEL /S
you probably want to do
DIR /S /B .#*.*
to list out the directories recursively
What about this:
FOR /R C:\FOLDER\SUBFOLDER %%G IN (.#*.*) DO DEL %%G