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!.
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 hoping someone can help me create a batch file to delete files that match a specific file format (tar) in a specified directory and its subdirectories that are older than 60 days BUT also keep a minimum number of matching files(may only have 1 file and if its older than 60 days we'll want to keep it), delete the rest. I found the example below that works for the root directory z:\zzz but it does not search/delete the matching files in its subdirectories. I'm not a developer so the example is cryptic to me.
#echo off
pushd z:\zzz\
for %%X in (tar) do (
set "skip=1"
for /f "skip=2 delims=" %%F in ('dir /b /a-d /o-d /tw *.%%X') do (
if defined skip forfiles /d -60 /s /m "%%F" >nul 2>nul && set "skip="
if not defined skip del "%%F"
)
)
At present your dir command looks like this:
dir /b /a-d /o-d /tw *.%%X
To get it to search the subdirectories you need to add the /s command and for safety I would change *.%%X to cd%\*.%%X
After looking at the code again think I can see two other parts of the code that need changing:
for /f "skip=2 delims=" %%F in needs to be:
for /f "skip=1 delims=" %%F in As you only want to keep one old backup.
if defined skip forfiles /d -60 /m "%%F" >nul 2>nul && set "skip="
Should be :
if defined skip forfiles /d -60 /s /m "*.%%X">nul 2>nul && set "skip="
So it includes sub directories and searches by the Current File Extension (As the /s being added to the dir command makes the %%F include the path as well as the file name, which was causing the forfiles command to not return any results.)
So with all the changes in place it should look something like :
#echo off
pushd "c:\backup"
for %%X in (tar) do (
set skip="1"
for /f "skip=1 delims=" %%F in ('dir /b /s /a-d /o-d /tw %cd%\*.%%X') do (
if defined skip forfiles /s /d -60 /m "*.%%X">nul 2>nul && set "skip="
if not defined skip del %%F
)
)
I've tested this on my laptop and it seems to work ok now. If you can try it and let me know how you get on.
I have a window directory with 300 folders inside of it and 4 levels of folders within each folder. I need a listing of folders only up to 2nd level in a txt file? I need the full path of each directory as well. I would like to use this a BAT file or CMD line prompt. Ex:
Jon Done
Test001
Tester002
Test003
Tester004
Can anyone help me to do this?
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
PUSHD "%sourcedir%"
FOR /f "delims=" %%a IN ('dir /b /a-d 2^>nul') DO ECHO %%~fa
FOR /f "delims=" %%a IN ('dir /b /ad') DO (
PUSHD "%%a"
FOR /f "delims=" %%x IN ('dir /b /a-d 2^>nul') DO ECHO %%~fx
popd
)
popd
GOTO :EOF
This should work - you would need to change the setting of sourcedir to suit your circumstances.
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
)
There is a particular folder that begins with a name such as SS followed by random characters. The names would be different every time and the only thing we are sure of is the folder begins with SS. How do we look if this folder has a .txt file inside in batch programming.
An idea :
#echo off
for /f "delims=" %%a in ('dir /b/ad ^|find /i "SS"') do set $Dir=%%a
dir /b/a-d *.txt %$dir%>nul
if %errorlevel% equ 0 echo File(s) found in "%$DIR%"
#ECHO OFF
SETLOCAL
SET "sourcedir=U:\sourcedir"
FOR /f "delims=" %%a IN (
'dir /b /ad "%sourcedir%\ss*" 2^>nul'
) DO (
FOR /f "delims=" %%h IN (
'dir /b /a-d "%sourcedir%\%%a\*.txt" 2^>nul'
) DO (
ECHO "%sourcedir%\%%a\%%h"
)
)
GOTO :EOF
should solve your problem - you need to change sourcedir to suit your system, obviously.
The code below check if the folder contain any .txt file:
#echo off
set "filePath="
for /D %%a in (SS*) do if exist "%%a\*.txt" do set "filePath=%%a"
if defined filePath echo File exists in folder %filePath%
If you want to check for a particular .txt file, just change *.txt by the appropriate name.