Wildcard with cmd files - for-loop

I am currently using this statement:
for /d %%X in (C:\Users\*) do (del %%X\Desktop\deleteme.txt )
Although I would like to use the "%%X" in to parts of this statement
e.g.
del %%X\%%X\deleteme.txt
How can this be done?
Any help would be greatly appreciated

It seems like you want to recursively delete files. This could be done by using the del command directly. By typing:
del deleteme.txt /s
deleteme.txt will be deleted from all subfolders. If that is not what you want you also could have your for loop call another batch file to perform the deletion from sub folders.
%%X will not work, as it gets fully expanded first.

Related

How to write a batch file to delete multiple specific files from different paths?

Each time before I start debugging, I need to delete some specific files from different paths. It's a tiresome process as I do it like a million times in a day. So I want to write a batch file that deletes all those at once without prompting.
The first path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\DD\servers\DefaultServer\tmp\
I want everything under this temp folder gone. Without the temp folder itself, of course.
The second path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\o.j2ee\drs\
I again want everything under this drs folder gone. Again without the drs folder itself, of course.
The third path is
C:\Users\irem\AppData\Roaming\JDeveloper\systemx\
This time I want to delete only the files with .lok extension under the systemx folder.
I tried to write something like this:
del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\DD\servers\DefaultServer\tmp\*.*?" /s
& del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\o.j2ee\drs\*.*?" /s
& del "C:\Users\irem\AppData\Roaming\JDeveloper\systemx\*.lok"
However it doesn't meet my expectations, it doesn't work.
I appreciate all the help. Thank you very much.
Perhaps something like this would do what you want:
#Echo Off
Set "srcPath=%AppData%\JDeveloper\systemx"
Set "tmpPath=DD\servers\DefaultServer\tmp"
Set "drsPath=o.j2ee\drs"
CD /D "%srcPath%" 2>Nul || Exit /B
Del /F /Q /A *.lok
For /D %%A In ("%tmpPath%\*" "%drsPath%\*") Do (RD /S /Q "%%A"
Del /F /Q /A "%%A\*.*")
I have used the paths you provided in your question, if those have changed, you can alter them by editing lines 2, 3 and 4 as necessary.

batch delete with directory variable - windows 7

I use the following batch file to delete unwanted files on several drives.
set /p DELPATH=<"C:\DELETE-ALL.txt"
for /f "usebackq delims=;" %%i in ("C:\DELETE-ALL.txt") do #del /q "D:\HFI\%DELPATH%\%%i" > C:\DELETE-ALL-4.txt 2>&1
... same command for other local and network drives.
The DELETE-ALL.txt looks like this:
mydirectory
TEST.xlsx
TEST2.xlsx
This works great. It deletes files in single directory. But now I need it to do more. I need the batch file to delete files in different directories. So, it boils down to how to change directory on the fly.
Any help will be greatly appreciated.
I answer you here because i can't comment now with my lower reputation.
I strongely recommend to use PowerShell or python or others program scripts to do this. Using windows batch, it will take you more time to find a good way and there may be no way to do such a little complex misson.
The answer turns out to be easier than I thought. Although my original question was for deleting files, I got it to work for rename. It should work for delete with little modification.
#(for /f "tokens=1,2 delims=;" %%A in ('"TYPE C:\RENAME-ALL.txt"') do (
#echo "%%A" | find /i "\"
#if errorlevel 1 (
RENAME "%%A" "%%B" >> C:\RENAME-ALL-4.txt 2>&1
) ELSE (
CD /D D:\mydirectory\%%A
)
)
)
The script looks for "\". If found, it assumes that line is a directory and change to the corresponding directory with "D:\mydirectory\" as a path prefix. Otherwise, it assumes the line contains file name. Since back slash is not allowed in filename, the assumption is safe.
Hope this will help other people.

While searching for .pst files FOR keeps looping when it reaches a hidden shortcut folder

The following command creates an infinite loop which is not what I want since I am iterating through files and it needs to end sometime...
Here is what I have:
cd C:\
FOR /R %i IN (*.pst) do #echo %i
See what happens is that when it reaches AppData and finds a .pst (in AppData\Local\Microsoft\Outlook) there is a shortcut folder inside AppData\Local called "Application Data" which loops back to AppData\Local but keeps adding it's name to the address like so:
%AppData%\Local\Application Data\Application Data\Application Data\Microsoft\Outlook\%filename%.pst
What could I add to my code to keep it from looping or much better to completely ignore shortcuts so that the loop ends when it finds all the files that I need?
-----------Edit-------------
This seems to do something similar:
dir /s /b *.pst
You can filter out reparse points with DIR /A-L.
However, using DIR /A-L /S won't work also, because reparse point contents are not reparse points, so, try this:
Instead of FOR use:
FindFiles.bat *.pst c:\
Create a FindFiles.bat file with:
#ECHO OFF
:GetDirFiles %1=Wildcard %2=Path
FOR %%f IN ("%~f2\%~1") DO ECHO %%~ff
FOR /F "DELIMS=" %%d IN ('DIR /B /AD-L "%~f2"') DO CALL :GetDirFiles %1 "%~2\%%d"
This will recursivelly get all directories which are not reparse points and echo items matching pattern for each directory.
Ok, I recommend you use forfiles which should be on your computer if your using windows 7. Type forfiles /? for more info. Try this:
forfiles /p "C:\" /s /m "*.pst" /c "cmd /c (Echo #path)"
That should work perfectly. Im looking in ways of doing this with a for /r loop. It probably involves a dir check in a for /r /d. Tell me if this works fine for you.
Mona

Changing file attribute using batch file

Is there away to change the file attribute of a file in multiple directory..? Right now, i have this code..
Please see details below..
attrib -r D:\deploy\A_qa\Sample1\*.* /S /D
attrib -r D:\deploy\B_qa\Sample1\*.* /S /D
Now, i want to simply it by adding a for loop statement.. Is that possible?
Thanks,
link
You can certainly iterate over subdirectories using a FOR /D loop. For instance, the following will iterate over subdirectories of D:\deploy\A_qa and display their full paths as well as their names only:
FOR /D %%D IN (D:\deploy\A_qa\*) DO (
ECHO Full dir path: "%%D"
ECHO Just the name: "%%~nxD"
)
You should probably issue two FOR /D loops, one for A_qa and the other one for B_qa, each with its own attrib command.
You might also want to read more about the FOR loop in the help, just run FOR /? from the command prompt.

Recursively create folder in specific directories

During a recent backup/restore cycle I've realized that I managed to omit the 'tmp' directories from within the '.svn' directories, and because of this I can't update my working copies. The problem goes away if I manually create a new, empty 'tmp' directory so I am looking for a way to recursively go through each folder, find '.svn' ones and create a 'tmp' folder inside them.
As I don't want to mess up the existing folders I thought I's ask for help before I did something silly :)
Comments/suggestions will be appreciated, thanks!
PS: This is on a Windows machine so sadly Bash and other unix utilities are not present.
The script above doesn't work on my on Windows 7 machine. The "dir /b /s .svn" doesn't get all dirs, I get a "File Not Found" error.
I changed the script to have /ad in addition to select directories only and that works! Here is the srcipt which works for me.
#echo off
for /f "usebackq delims=" %%I in (`dir /ad /b /s .svn`) do (
echo Fixing %%I...
mkdir "%%I\tmp"
)
Depends on how many there are.
List the directories with
dir/B/S .svn >dirs.bat
Edit dirs.bat in your editor of choice. Add md at the beginning of each line (since each line begins with something like C: you can use a fairly dumb editor - including notepad - to change C: to md C: ). Add /tmp to the end of each line (replace .svn with .svn\tmp). Save. Run the BAT file
Job done.
Here's how to automate the entire process. Put the following in a file like fixtmp.cmd:
#echo off
for /f "usebackq delims=" %%I in (`dir /b /s .svn`) do (
echo Fixing %%I...
mkdir "%%I\tmp"
)

Resources