I'm in D:\User Profiles\ and I need to delete directorys located in
D:\User Profiles\---USERNAME---\UPM_Profile\AppData\Local\Mozilla\Firefox\Profiles\*.default
Now here's the question. How can I do this dynamic ?
If I type
dir /ad /b /s D:\User Profiles\*\UPM_Profile\AppData\Local\Mozilla\Firefox\Profiles\*.default
it fails.
---USERNAME--- and *.default needs to be dynamic.
Any ideas `?
Something like:
#echo off
for /d %%i in ("D:\User Profiles\*") do (
call :remove_dirs %%i
)
goto :eof
:remove_dirs
echo %1
for /d %%j in ("%1\UPM_Profile\AppData\Local\Mozilla\Firefox\Profiles") do rmdir %%j
goto :eof
Related
This is a sample code that allows me to delete all folders with the name ".RemoveAsap" attached to them
#echo on
set dir="\\TestPC2\c$\Users"
FOR /D /R %dir% %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
pause
exit
Simply running the code as is runs perfectly but when I try to make the code more interactive, I get the error
#echo on
cd C:\Users\User1\Desktop\Test\
TYPE con >> LowDASD.txt
For /F %%A in (LowDASD.txt) do echo "\\%%A\c$\users\" >> LowDASD2.txt
set "LwDs"="LowDASD2.txt"
FOR /D /R "%LwDs%" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
pause
LowDASD2.txt would be the address/ directory location where the directories will be deleted, IE \\TestPC2\c$\Users
The code does not delete anything or give an error that "the path is too long" at least it was doing that with the previous variations that I was trying. If someone can help me with this, i would greatly appreciate it.
Try using FORFILES, instead of the command FOR, this way you can make it work like this:
:: forfiles /p "folder_location" 'args' '/c "cmd /c del /f /q #path"'
:: So...
cd C:\Users\User1\Desktop\Test\
TYPE con >> LowDASD.txt
For /F %%A in (LowDASD.txt) do echo "\\%%A\c$\users\" >> LowDASD2.txt
set "LwDs=LowDASD2.txt"
forfiles /p %LwDs% /s /c "cmd /c del /f /q #path"
:: You can use '/d -90' to delete files older than 90 days in the folder
FOR /D /R "%LwDs%" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
Simply will not work, as %LwDs% is a filename.
FOR /F "usebackq delims=" %%j in ("%LwDs%") do FOR /D /R "%%j" %%X IN (*.RemoveAsap) DO RMDIR /S /Q "%%X"
You would think might work - %%j being assigned to each entry in the %LwDs% file in turn; usebackq used because the filename is "quoted" (see for /? from the prompt for documentation)
But it doesn't - the for /d /r syntax doesn't accept metavariables...
So - try
FOR /F "usebackq delims=" %%j in ("%LwDs%") do set "target=%%j"&call :expunge
Where expunge is an internal subroutine. The colon is required
:expunge
echo target="%target%"
FOR /d /r "%target%" %%X IN (*.RemoveAsap) DO echo RMDIR /S /Q "%%X"
echo ====================
goto :eof
An internal subroutine should be placed after an unconditional goto, which should in your case follow the pause
pause
goto :eof
Where :eof (compulsory colon again) is defined as the physical end-of-file and should not be used as a user-label. Reaching physical end-of-file returns from a call.
Always verify against a test directory before applying to real data.
Note that the rmdir is merely being echoed for testing purposes - remove the echo keyword after testing to activate.
=== Extension
The full code should thus be
#echo on
set dir="\\TestPC2\c$\Users"
FOR /F "usebackq delims=" %%j in ("%LwDs%") do set "target=%%j"&call :expunge
pause
exit
:expunge
echo target="%target%"
FOR /d /r "%target%" %%X IN (*.RemoveAsap) DO echo RMDIR /S /Q "%%X"
echo ====================
goto :eof
I need to go through a list to copy the specified files into another location.
This is the used .bat file:
for /f "delims=" %%i in ('C:\Users\Documents\test\data\list_1.4.txt') do copy "C:\Users\Documents\test\data\Golden\%%i" "C:\Users\Documents\test\data\data_1.4"
But this does not work.
Any help?
I tried this too
#echo off
set src=C:\Users\Documents\test\data\Golden
set dst=C:\Users\Documents\test\data\data_1.4
set file=C:\Users\Documents\est\data\list_1.4.txt
for /F "usebackq tokens=*" %%a in ("%file%") do xcopy "%src%\*%%~a*" "%dst%" /C /Q /H /R /K /Y 1>nul 2>nul
pause
Based on your comment, the file content is something like:
"C:\Users\Documents\test\data\Golden\file1.txt"
"C:\Users\Documents\test\data\Golden\file2.pdf"
"C:\Users\Documents\test\data\Golden\file3.exe"
If that is the case, then:
#echo off
set "dest=C:\Users\Documents\test\data\data_1.4"
set "file=C:\Users\Documents\est\data\list_1.4.txt"
for /f "usebackq delims=" %%i in ("%file%") do (
if exist "%%~fi" copy /Y "%%~fi" "%destination%"
)
I have a folder with several subfolders. Structure is like:
C:\foo
C:\foo\web.foo
C:\foo\web.bar
C:\foo\win.foo
C:\foo\win.bar
C:\foo\mobile.foo
C:\foo\mobile.bar
I sometimes wish to delete the folders with its containing files with following batch script:
rmdir C:\foo /s /q
Here it didn't matter that the whole folder C:\foo was deleted completely.
But now I only want to delete only the subfolders of C:\foo with its containing files, which DO NOT start with "web.".
Do you have any good solution for this?
The following should do the trick, note it's a batch file using the current directory:
#echo off
for /F "delims=" %%D in ('dir /B /AD ^| findstr /V "^web."') do (
echo rmdir %%D /s /q
)
If it's okay remove the echo in front of rmdir.
The dir command just list directory names because of /AD and use a simple name output because of /B. To search on the beginning use findstr with /V. For negation use ^. Further the pipe symbol needs to be escaped ^|.
If you want a dynamic batch script that uses arguments you can use the following, call it via batchname.bat "C:\foo" web. (if it's okay remove the echo in front of rmdir.):
#echo off
set INVARGS=0
if [%1] == [] set "INVARGS=1"
if [%2] == [] set "INVARGS=1"
if %INVARGS% == 1 (
echo echo %0 ^<directory^> ^<directory_prefix^>
goto eof
)
set "folder=%1%"
set "prefix=%2%"
pushd "%folder%"
echo List of folders that should be deleted:
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo "%cd%\%%D"
)
popd
:choice
echo.
set /P "c=Are you sure you want to continue [Y/N]?"
if /I "%c%" EQU "Y" goto yes
if /I "%c%" EQU "N" goto eof
goto :choice
:yes
echo.
pushd "%folder%"
for /F "delims=" %%D in ('dir /B /AD ^| findstr /v "^%prefix%"') do (
echo rmdir %%D /s /q
)
popd
:eof
This will remove all files begin with web.
#echo off
setlocal EnableDelayedExpansion
for /f %%F in ('dir /b /s') do (
set "name=%%~nF"
if /i not "!name:~0,4!" == "web." (
rm !name!
)
)
I want to delete all directories with a specific name. E.g. "vendor"
D:\Data\Project1\Dir\vendor
D:\Data\Project2\AnotherDir\AnotherDir\vendor
D:\Data\Project3\vendor
This is what I have at this moment. This lists all the folders, and subfolders.
for /d %%a in (*) do dir /ad /on /s /b %%a
Now I wish to put one path in a variable, and check whether the directory is equal to a name (for example "vendor"). But I can't find how.
Can you help me?
Regards,
Demian
This is my working solution if you have RimRaf installed:
#ECHO off
setlocal enableDelayedExpansion
:: Folders where the batch program doesn't need to search.
set skipTheseFolders=Decleir Pinokkio
:: All the folders that need to be deleted
set foldersToDelete=vendor node_modules
for /d /r %%d in (*.*) do (
#ECHO %%d
set folder=%%~nxd
set canI="true"
:: For loop to skip folders
for %%s in (%skipTheseFolders%) do (
:: Need to be implemented
set canI="true"
)
IF !canI! =="true" (
for %%l in (%foldersToDelete%) do (
:: If the folder is one of the folders To Delete
IF "!folder!" == "%%l" (
#echo %%d
cd %%d
cd ..
:: Using Rimraf because when you delete node_modules, normal delete won't work
start /B rimraf !folder!
)
)
)
)
The DIR command has an unfortunate limitation that it cannot recursively list specific folder names. The simplest solution is to list all folders, and use FINDSTR to filter out all but the folders that match.
dir /ad /on /s /b | findstr /iec:"\vendor"
Then you simply iterate the results using FOR /F and add your RD command
for /f "eol=: delims=" %%F in
'dir /ad /on /s /b ^| findstr /iec:"\vendor"'
) do rd /s /q "%%F" 2>nul
You can try this :
#Echo OFF
set "Folder=D:\Data"
set "FolderString=vendor"
Setlocal Enabledelayedexpansion
FOR /f "tokens=*" %%F IN ('dir /b /s /ad %Folder%\ ^| find "%FolderString%"') DO (set var="%%F"
ECHO rd /s /q !var! && rd /s /q !var!)
EndLocal
pause
Ex: doc_1.2.3.jar
Expected output: doc.jar
dir /b
for %%f in (*_*.jar) do call :ProcessFile %%f
goto :finished
:ProcessFile
set str=%1
rename %1 %str:_=%
goto :eof
:finished
echo ----
dir /b
I tried using wildcards, but it doesn't seem to work
rename %1 %str:_*.jar=.jar%
A way :
#echo off
for /f "tokens=1,2 delims=_" %%a in ('dir /b/a-d *.jar') do rename "%%a_%%b" "%%a.jar" 2>nul