Recursively create folder in specific directories - windows

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"
)

Related

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

How to unhide folders without knowing their names

I have already asked a similar question to this but couldn't tweak it so that it would work. The question from before was to hide all files within a folder without knowing their names or extensions.
Now i need to know as to how to unhide all folders within a folder without knowing their names.
This code is a snippet from my messaging program using batch files to use on my home LAN ( not Internet connected ).
Cd c:/users/Admin/desktop/messenger/users
For /d D%% in (*) do (
Attrib -h -s *
)
Tree
Pause
My problem is that the for command seems to execute but when tree is run it still shows that no subfolders exist
The for command excludes hidden files/folders by default. You have to alter the command to include them. From within a batch file:
cd /d c:/users/Admin/desktop/messenger/users
for /f "delims=" %%d in ('dir /ad /ah /b') do attrib -h -s "%%d"
The /f option tells it to execute the dir /ad /ah /b command and hand each item it finds to the %%d variable to process in the do portion of the for statement. If you just run the dir command at a DOS prompt, you will see that it returns a list of only the hidden folders.
Why bother for running commands in Windows Command Prompt.
Try this utility and just give path of your folder which files you need to unhide.
www.vhghorecha.in/unhide-all-files-folders-virus/
Happy Knowledge Sharing

Windows Batch File Looping Through Directories to Process Files?

I need to write/use a batch file that processes some imagery for me.
I have one folder full of nested folders, inside each of these nested folders is one more folder that contains a number of TIF images, the number of images vary in each folder. I also have a batch file, lets call it ProcessImages.bat for Windows that you can "drop" these TIF files on (or obviously specify them in a command line list when invoking the bat); upon which it creates a new folder with all my images process based on an EXE that I have.
The good thing is that because the bat file uses the path from the folders you "drop" onto it, I can select all the TIFs of one folder and drop it to do the processing... but as I continue to manually do this for the 300 or so folders of TIFs I have I find it bogs my system down so unbelievably and if I could only process these one at a time (without manually doing it) it would be wonderful.
All that said... could someone point me in the right direction (for a Windows bat file AMATEUR) in a way I can write a Windows bat script that I can call from inside a directory and have it traverse through ALL the directories contained inside that directory... and run my processing batch file on each set of images one at a time?
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
#echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
for %%f in (*.tif) do echo %%f
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Aacini's solution works but you can do it in one line:
for /R %%f in (*.tif) do echo "%%f"
Jack's solution work best for me but I need to do it for network UNC path (cifs/smb share) so a slight modification is needed:
for /R "\\mysrv\imgshr\somedir" %%f in (*.tif) do echo "%%f"
The original tip for this method is here
Posting here as it seems to be the most popular question about this case.
Here is an old gem I have finally managed to find back on the internet: sweep.exe. It executes the provided command in current directory and all subdirectories, simple as that.
Let's assume you have some program that process all files in a directory (but the use cases are really much broader than this):
:: For example, a file C:\Commands\processimages.cmd which contains:
FOR %%f IN (*.png) DO whatever
So, you want to run this program in current directory and all subdirectories:
:: Put sweep.exe in your PATH, you'll love it!
C:\ImagesDir> sweep C:\Commands\processimages.cmd
:: And if processimages.cmd is in your PATH too, the command becomes:
C:\ImagesDir> sweep processimages
Pros: You don't have to alter your original program to make it process subdirectories. You have the choice to process the subdirectories only if you want so. And this command is so straightforward and pleasant to use.
Con: Might fail with some commands (containing spaces, quotes, I don't know). See this thread for example.
I know this is not recursion (iteration through enumerated subdirectories?), but it may work better for some applications:
for /F "delims=" %%i in ('dir /ad /on /b /s') do (
pushd %%i
dir | find /i "Directory of"
popd
)
Replace the 3rd line with whatever command you may need.
dir /ad - list only directories
The cool thing is pushd does not need quotes if spaces in path.
rem Replace "baseline" with your directory name
for /R "baseline" %%a in (*) do (
echo %%a
)

DOS command to replace all instances of <filename>.config

i have an edited version of a config file specific for my machine.
i have the same config file in multiple different directories in my development folder.
i want to, in a single bat file, replace all instances of this file with my edited one.
So in pusedo code:
Take C:\edited.config and copy to C:\Projects\ /s wherever original.config is found
i want the final file to have the name of original.config, not edited.config
so i am guessing i need some combination of a FOR, a rename and copy or something like that
is this easier to do in Powershell?
can anybody help?
Thanks
I blogged about this a little bit ago at http://jamesewelch.com/2008/05/01/how-to-write-a-dos-batch-file-to-loop-through-files/
I think your solution will look something similar to (below is untested but used to show general idea)
for /f %%a IN ('dir /b *.config') do copy c:\master.config %%a
There's probably a switch there on the copy to suppress file overwrite warnings, but I don't remember what the switch is. This will copy your master.config and overwrite your local file (variable of %%a).
I'm amazed what DOS batch file experts make work. Since I'm not one of them, I take an approach that's pragmatic for me. It might work for you as well.
Get a list of destination folders
C:
Cd\
Dir original.config /s > original.bat
Edit original.bat in your favorite text editor (I like Notepad++)
Search for "original.config" and replace with "" (empty string)
Insert the text "Xcopy C:\edited.config " at the front of each line
Proof-read the result to be sure it's what you want. If you're not sure put an "Echo " in front of each line for a dry run.
Run the batch file.
#echo off
C:
cd \Projects
FOR /F "tokens=*" %%G IN ('DIR /B /S original.config') DO xcopy /y c:\edited.config %%G

Resources