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

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

Related

Batch file that searches for a folder with the same name as a local file and moves said file to the located folder not working

So I am trying to create a batch file that will take a pdf file in the same directory as the batch file and output the file name (sans extension). I used this code to accomplish this:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"
This works fine. The next step is to search another directory and find a directory within the searched directory whose name matches the output of the above code (stored in the %name% variable). Here's what I tried:
dir "P:\Accounting\Acc Pay" | find %name% | set "loc=%%~dp"
The goal of the above code was to find only the directories that had the same name as the original pdf file and then set the drive and path of the output to a variable %loc%. I think this is where I messed up.
Once the path to the folder is set to %loc%, I then am supposed to finish with this line:
move .\*.pdf %loc%
This would take all the pdf files (there will only be one in the directory at once) in the directory with the batch file and move it to the path currently stored in the %loc% variable.
In total the code looks like this:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do set "name=%%~nG"
for /r %%A in ('dir "P:\Accounting\Acc Pay" | find %name%') do set "loc=%%~dpA"
move .\*.pdf %loc%
However, the code seems to move the pdf file into the same location it was already in (ie the folder with the batch file). I assume the %loc% variable is not working properly. Any help much appreciated.
Like #Magoo said (^|). And you surely want to add the following switches for the dir command: /b for "bare format" (name only) and /ad for "Attribute Directory" to return folder names only. find needs its's argument quoted, and for safety, the destination for the move command should also be quoted. Your find could benefit from /i to make it case-insensitive.
I personally would do it with nested loops to avoid creating superflouos variables:
#echo off
for /r "C:\Users\me\Test Folder" %%G in (*.pdf) do (
for /r %%A in ('dir /b /ad "P:\Accounting\Acc Pay" ^| find /i "%%~nG"') do (
move "%%G" "%%~dpA"
)
)
Bonus: should there be more than one .pdf file (maybe after the Weekend or Holidays), this would process all of them correctly in one go.
Depending on your naming structures, consider replacing find /i "%%~nG" with findstr /iblc:"%%~nG" (see findstr /?to find out what the switches mean)
(Note to prevent confusion: findstr is the only command (as far as I'm aware) that supports concatenating switches into one. /iblc is the same as /i /b /l /c)

How to list all folders on specified drives with folder name first then gap then full path to folder?

dir i:\ j:\ k:\ /b /s /a:-D >c:\Users\Jason\Desktop\allFolderFilesOn_I_J_K.txt
This works great, but I am after a custom output, foldername full_path
Is this possible and if so how do I achieve this?
If I understand correctly, you don't want any files, but for every folder instead of C:\windows\system32 you want an output like system32 C:\Windows\
This can be done with the following command:
(for /d /r "D:\" %a in (*) do #echo %~nxa %~dpa)>c:\Users\Jason\Desktop\allFolderFilesOn_I_J_K.txt
where /d means "Directories only"
/r "Recursive" ("down the folder tree")
"D:\" the starting point
%~nxa the last element (the folder name)
%~dpa the drive/path to the folder
Note: this is command line syntax. For use in a batch file, replace each % with %%

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.

Is there a way to find an element with only the final name in windows batch

I'm a huge noob in windows batch and i would like to know if there is a way to find a file with the final name.
For example if I want to run a file call "myBatch.bat" but I don't know where exactly it is on my computer
Is there a command like
c:/*/myBatch.bat
You can recursively search for files using for /r. From a cmd console:
for /r "C:\" %I in (*mybatch.bat) do #"%~I"
From within a .bat script:
for /r "C:\" %%I in (*mybatch.bat) do call "%%~I"
As aschipfl comments above, this could return a false positive if you have any files named similarly, but prefixed. For an additional sanity check you could add an if statement.
for /r "C:\" %%I in (*mybatch.bat) do if /i "%%~nI"=="mybatch" call "%%~I"
In any case, for /r is slightly more efficient than for /f.

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