for /f "tokens=* delims=" %%a in ('dir "recordings.txt" /s /b') do (
echo %%a
)
Is this the correct format to look on the Z: drive for files in the recordings.txt?
Tried powershell don't have permissions on the server
Contents of the file just have the file name / extension
3030009948_3030009912_df1389947f0fb80d62832122.sasf
The Directory structure of Z: is as follows
MM\dd\hh\mm\
recordings.txt is on the Desktop of my userprofile
I also need the paths of the found files
This should read recordings.txt on your desktop and create recordings-results.txt in the same place with the full path to every file inside it.
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
del "%userprofile%\desktop\recordings-results.txt" 2>nul
for /f "usebackq delims=" %%a in ("%userprofile%\desktop\recordings.txt") do (
echo finding "%%a"
findstr /i /c:"%%a" "%temp%\results.tmp" >>"%userprofile%\desktop\recordings-results.txt"
)
del "%temp%\results.tmp"
pause
This should be even faster:
#echo off
dir /b /s /a-d "z:\" >"%temp%\results.tmp"
findstr /i /g:"%userprofile%\desktop\recordings.txt" "%temp%\results.tmp" >"%userprofile%\desktop\recordings-results.txt"
del "%temp%\results.tmp"
pause
Dir is to look into a directory if you want to get a value from a file use :
for /f "tokens=*" %%a in ('type "Z:\recordings.txt"') do (
echo %%a
)
Related
I have a Windows CMD script that has a bug. The script is supposed to match the first 8 digits (the date) of a file with a directory titled with the same first 8 digits (the date). If successful, the file is moved into that directory & placed in a subfolder (called 'portfolio'). However, the error File not Found is returned.
file: 20230202_example.jpg
directory: 20230202_winter-holiday/portfolio
...the CMD file:
#echo off
for /f "delims=" %%a in ('dir /b /a-d') do (
set "filename=%%a"
set "first8=!filename:~0,8!"
for /f "delims=" %%b in ('dir /b /a-d *%first8%*') do (
if /i "!filename!" neq "%%b" (
move "!filename!" "%%b\portfolio\!filename!"
)
)
)
If I interrogate the directory in Command Prompt:
dir /b /a-d
...I get a full list of the files contained. When the script is run from Command Prompt, for each file contained I get:
File Not Found
Based upon your target file and directory names, you could probably do it without defining variables and therefore the need to delay variable expansion.
Example:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
^| %SystemRoot%\System32\findstr.exe /R /C:"^202[0123]1[012][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]0[123456789]_."
/C:"^19[789][0123456789]0[123456789][12][0123456789]_."
/C:"^19[789][0123456789]0[123456789]3[01]_." /C:"^202[0123]0[123456789]3[01]_."
/C:"^19[789][0123456789]1[012]0[123456789]_."
/C:"^19[789][0123456789]1[012][12][0123456789]_."
/C:"^19[789][0123456789]1[012]3[01]_." /C:"^20[01][0123456789]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789]0[123456789]_." /C:"^202[0123]1[012]3[01]_."
/C:"^20[01][0123456789]0[123456789][12][0123456789]_."
/C:"^20[01][0123456789]0[123456789]3[01]_."
/C:"^20[01][0123456789]1[012]0[123456789]_."
/C:"^20[01][0123456789]1[012][12][0123456789]_."
/C:"^202[0123]0[123456789]0[123456789]_." /C:"^202[0123]1[012]0[123456789]_."
/C:"^202[0123]0[123456789][12][0123456789]_."
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1
I decided to use a little more accuracy in the dates, (it's not perfect because it has no knowlege of how many days are in each month of any year, but should cover dates between 19700101 and 20231231). If you don't want that, you could simplify it by just removing lines 4 through 17:
#Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F "Delims=" %%G In ('Dir "????????_*" /A:-D /B 2^>NUL
') Do For /F "Delims=_" %%H In ("%%~nG") Do For /F "Delims=" %%I In ('
Dir "%%H_*" /A:D /B 2^>NUL'
) Do %SystemRoot%\System32\Robocopy.exe . "%%I\portfolio" "%%G" /Mov 1>NUL 2>&1
I'm creating simple batch script for deleting all user configs of specific application and I'm still failing on the last step where I'm trying to get all subfolders with specific prefix...
This is what I have right now:
#echo off
chcp 1250
SET appUserConfigDirectory=\AppData\Local\CompanyName
SET appConfigFolderPrefix=AppName.exe_Url
:: get parent folder of user folders
for %%d in (%USERPROFILE%) do SET userprofilesFolder=%%~dpd
SETLOCAL ENABLEDELAYEDEXPANSION
:: going through all user folders
for /F "delims=" %%d in ('dir %userprofilesFolder% /A:D-R-H-S /b') do (
:: set full name of CompanyName folder in user AppData
SET appConfigParentFolder=%userprofilesFolder%%%d%appUserConfigDirectory%
IF EXIST !appConfigParentFolder! (
:: There is a problem with dir command, it's says File not found even if subfolder with this prefix exists and print all subFolder no metter it's name...
for /F "delims=" %%i in ('dir !appConfigParentFolder! /A:D /b %appConfigFolderPrefix%*') do (
echo %%i)))
I find the way how to do it. The right dir command with prefix should be:
dir !appConfigParentFolder!\%appConfigFolderPrefix%* /A:D /b
So the full version of this sample is:
#echo off
chcp 1250
SET appUserConfigDirectory=\AppData\Local\CompanyName
SET appConfigFolderPrefix=AppName.exe_Url
for %%d in (%USERPROFILE%) do SET userprofilesFolder=%%~dpd
SETLOCAL ENABLEDELAYEDEXPANSION
for /F "delims=" %%d in ('dir %userprofilesFolder% /A:D-R-H-S /b') do (
SET appConfigParentFolder=%userprofilesFolder%%%d%appUserConfigDirectory%
IF EXIST !appConfigParentFolder! (
for /F "delims=" %%i in ('dir !appConfigParentFolder!\%appConfigFolderPrefix%* /A:D /b') do echo %%i
)
)
Please try with something like this:
forfiles /S /M !appConfigParentFolder! /C "cmd /c if #isdir==TRUE rmdir #path"
It removes every subdirectory, which equals !appConfigParentFolder!.
I have a directory that has a 10 sub-directories. Each of these holds different .bak files. I'm trying to create a script that will check to see if X number of files are there and if the number of files exceeds X, it deletes the oldest file. In other words, I want 20 iterations of a .bak file. When the 21st one shows up, I want the batch file to delete the oldest one.
Is this possible?
If so, can I create a single script that looks in all the sub-directories?
Thanks in advance.
Two options included. The first one will leave %maxFiles% bak files under each of the folders. The second one (windows Vista or later OS is required as robocopy is used to obtain the sorted list of files) will leave %maxFiles% in total
#echo off
setlocal enableextensions disabledelayedexpansion
set "rootFolder=%cd%"
set "maxFiles=20"
rem Option 1 - Keep %maxFiles% inside each of the subfolders
for /d /r "%rootFolder%" %%z in (*) do for /f "skip=%maxFiles% delims=" %%a in (
'dir /tc /o-d /a-d /b "%%~fz\*.bak" 2^>nul'
) do echo del "%%~fz\%%~nxa"
echo ------------------------------
rem Option 2 - Keep %maxFiles% in total under all the subfolders
for /f "skip=%maxFiles% tokens=2,*" %%a in ('
robocopy "%rootFolder%" "%rootFolder%" *.bak /l /nocopy /is /s /njh /njs /ndl /nc /ns /ts
^| findstr /v /r /e /i /c:"%rootFolder:\=\\%\\[^\\]*"
^| sort /r
') do echo del "%%b"
del commands are only echoed to console. If the output is correct, remove the echo command to remove the files
assumes that you want to check the number of files from the parent directory.Can be done also for each sub-directory.
#echo off
setlocal
set "max_number_files=20"
set "parrent_dir=c:\whatever_you_need"
set "extension=.bak"
pushd "%parrent_dir%"
set "count=0"
setlocal enableDelayedExpansion
for /f "delims=" %%a in ('dir /s /b /a:-d /o:-d /t:c *%extension%') do (
set /a count=count+1
if 1!count! GTR 1!max_number_files! (
rem --- remove the echo to activate deletion
echo del /q /f "%%~a"
)
)
popd
endlocal
endlocal
This will check each folder under d:\base\folder and if there are more than 20 *.bak files it will remove the oldest ones so only 20 *.bak file remain, in each folder.
Test it on some sample folders.
#echo off
for /d /r "d:\base\folder" %%a in (*) do (
pushd "%%a"
for /f "skip=20 delims=" %%b in ('dir /b /a-d /o:-d *.bak ') do del "%%b"
popd
)
I'm triying to get a list of files of subfolders in a inverse order using this:
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f)
I'm getting this result
folder1\file2, folder1\file, folder2\file2, folder2\file1, folder3\file2, folder3\file1
And I want to get the reverse order in folders and files, not only files of folders. Something like this
folder3\file2, folder3\file1, folder2\file2, folder2\file1\, folder1\file2, folder1\file1
How can I do this?
#echo off
for /f "tokens=*" %%f in ('dir /s /b /o-n') do (echo %%f >> tempfile.123)
C:\Windows\System32\sort.exe /R tempfile.123
del tempfile.123
This just echoes your files to tempporary file and then revereses it. Sorry for the full path to sort.exe but I have cygwin installed. In case you want proper temporary file name I reccomend this http://unserializableone.blogspot.com/2009/04/create-unique-temp-filename-with-batch.html
try this
for /f "tokens=*" %%f in ('dir /s /b /o-n')
do (
SET OUTPUT=%OUTPUT%, %%f
)
echo %OUTPUT%
run a first loop to get first level subdir then run you command on each of them
#echo off
for /f "tokens=*" %%f in ('dir c:\temp\so\ /b /o-n /AD') do (call :filesOf %%f)
:next
echo next
pause
:filesOf
echo "files for ******** %1 *********"
if ("%1"=="") goto next else(
for /f "tokens=*" %%i in ('dir c:\temp\so\%1 /s /b /o-n ') do echo "***%%i***"
)
it will be difficult to handle multi level subdirs tough
If I have a file named " mic.txt" on a Hard drive,
What are the codes that find this file and delete it ??
If batch files can't do this, What are there any other ways ?
delete_my_file.bat
#echo off
set file_to_delete=mic.txt
set dir_to_look_in=C:\
:: Starting loop
for /f "tokens=1* delims=" %%f in ('dir /s /o /b "%dir_to_look_in%" ^| findstr "%file_to_delete%"') do (
echo INFO: Deleting: %%f
del /q "%%f"
)