executing all the .reg files from batch file - batch-file

I have following script that executes all the .reg files in the current directory. the problem is i also have sub directories in that folder and i want to execute those reg files as well. I am not sure what switch i am supposed to use. i tries using /s which returns all the .reg files of the drive.
this is how my batch file looks like:
rem #echo off
cls
SET folder=%~dp0
for %%i in ("%folder%*.reg") do (regedit /s "%%i")
echo done
pause
EXIT
This is how file/directory structure looks like:
Folder1
batchfile.bat -> user double clicks or executes from command prompt
1.reg
2.reg
3.reg
Folder2
4.reg
5.reg
what am i missing here?

I think that you need a recursive for loop using the /R switch:
for /R "%folder%" %%i in (*.reg) do (regedit /s "%%i")

You didn't actually say what your problem was, so instead of fixing your script here's what I would have done:
#echo off
set folder=%~dp0
for /f "usebackq" %%i in (`dir /s /b *.reg`) do (
regedit /s "%%i"
)
And the /s script on regedit is for silent mode. It tells regedit not to ask the user for confirmation with a dialog box. The for loop is what causes the batch file to loop through all subdirectories, not the /s switch on regedit.
From Microsoft's Knowledge Base:
[/s|-s]
When a filename is specified on the command line, this switch is used to
suppress any informational dialog boxes that would normally be displayed.
This is useful when the Setup program for an application wants to execute
REGEDIT.EXE with a .REG file, but does not want the user to be confused by
any dialog boxes that are displayed.

Related

how to zip all files individually in all subfolders and remove original file after

what im looking for is a .bat file code to zip files individually in all subfolders in the current folder and then delete the of files after, to exclude already zipped/compressed files, what i dont want is folders to be zipped and i want the files to keep there name when zipped
i have a bunch of folders/files and the only code i found
#ECHO OFF
FOR %%i IN (*.*) DO (
ECHO "%%i" | FIND /I "batch zip files.bat" 1>NUL) || (
"c:\Program Files\7-Zip\7z.exe" a -tzip "%%~ni.zip" "%%i"
if %ERRORLEVEL% == 0 del "%%i"
)
)
zips all files in the current directory and doesnt touch subfolders
i'd appreciate it if anyone can do this for me as i can save a ton of space with all files zipped
The first issue you have with your provided code is that your For loop is only parsing files in the current directory, there is no recursion into subdirectories. To parse files within the subdirectories, I'd advise that you use a For /F loop, with the Dir command using its /B and /S options. I would also advise that you include the attribute option, /A, which will include every item, then omit those which you're not interested in. For instance, it's unlikely that you want to zip the directories, hidden files, reparse points, or system files. You can do that by excluding those attributes, /A:-D-H-L-S. To learn more about the For command, and the Dir command, open a Command Prompt window, type for /?, and press the ENTER key. You can then do the same for the Dir command, i.e for /?. As you have not defined a working directory at the start of your script, it will run against every file and directory in whatever is current at the time you run it. Because your code has a line excluding a file named batch zip files.bat, I'm going to assume that is the name of your running script, and that your intention is to therefore run the script against everything in the tree rooted from the same location as the batch file itself. To ensure that is always the case, for safety, I've defined that directory as the current directory from the outset, using the CD command, CD /D "%~dp0". %0 is a special batch file argument reference to itself, to learn more about this please take a look at the output from both call /?. You can also learn about the CD command entering cd /?, in a Command Prompt window too. To also omit your batch file, as you don't want it to be zipped and deleted, I've piped the results from the Dir command through FindStr, printing only items which do not exactly match the case insensitive literal string %~f0 (expanding to the full path of the batch file itself). Additionally, I've piped those results through another findstr.exe command to omit any files already carrying a .zip extension, as there's no point in zipping files which already zip files. (Please note however, that for more robust code, you should really check that those are zip archives and not just files carrying a misleading extension). The results from those commands are then passed one by one to the Do portion which includes your 7z.exe command. I've assumed at this stage, that your intention was to save the zipped archives to the same location as the originating files. To do that I've used variable expansion on %%G to stipulate its directory, path, and name, %%~dpnG, (see the usage information under for /? to recap). Upon successful completion of the archiving process, the original file will be deleted, to do that I appended the -sdel option to your original command string. Please be aware that you may want to include additional options, should you wish to update existing zip files etc. (use "%ProgramFiles%\7-Zip\7z.exe" -? in a Command Prompt window to see them). As I've not mentioned it previously, at the beginning of the script, I made sure that extensions were enabled. Whilst it is the default option, it's safer to be sure, as variable expansion and the commands CD, and For can be affected, if they're not.
Here's the code as explained above:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /I /L /V /X "%~f0" ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnG.zip" "%%G" -sdel
Looking at your question, which has changed from what you'd asked initially, you appear to not be interested in the files of the batch file directory any more, "zip files individually in all subfolders in the current folder". For that reason, I've provided the following alternative, methodology.
The difference is that I first of all use a For loop to include only directories in the current working location, /A:D-H-L-S, before running the same method used in my previous example, but with one difference. As we're now no longer zipping files in the current working directory, we can remove the findstr.exe command filtering out the running batch file:
#Echo Off
SetLocal EnableExtensions
CD /D "%~dp0"
For /F "EOL=? Delims=" %%G In ('Dir "*" /A:D-H-L-S /B 2^> NUL'
) Do For /F "EOL=? Delims=" %%H In ('Dir "%%G" /A:-D-H-L-S /B /S 2^> NUL ^|
%SystemRoot%\System32\findstr.exe /E /I /L /V ".zip"'
) Do "%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~dpnH.zip" "%%H" -sdel
Please be aware, that my answers above are to essentially correct your code attempt, and not a personal recommendation for speed, or in performing the task laid out in your question. Additionally, I have no idea what will happen if any of those files are in use/locked, and have made no attempt at checking for such scenarios.

Batch Script to copy folder from Server to user desktop in a network

My goal is to create a batch script to copy a folder with subfolders to user desktop with overwrite option and minimized of command prompt.
I am pushing the script through Group policy in user start up screen.
However I am getting an error when running the script locally. Not sure what I am missing in the script..
#echo off
#cls
if not "%minimized%"=="" goto :minimized
set minimized=true
start /min cmd /C "%~dpnx0"
goto :EOF
:minimized
echo Your Source Path:
set INPUT1=\\X.X.X.X\Test\TMS\
echo Your Destination Path:
set INPUT2=C:\Users\%Username%\Desktop\
xcopy %INPUT1% %INPUT2% /y /s
:minimized
You mentioned folder, so I am writing this assuming you want to create the TMS folder with content on the desktop.
I would try something like this inside of you minimized label. This is untested as I have no Network drives to test with.
for /f "tokens=2" %i in ('net use * "\\X.X.X.X\Test\TMS\" ^| findstr /i Drive') do set "tmpDr=%%i"
mkdir "%USERPROFILE%\Desktop\TMS" >nul
xcopy "%tmpDir%\*" "%USERPROFILE%\Desktop\TMS" /s /y
net use /d %tmpDir% >nul
Some things to note in your code. You have 2 minimized labels, you need to enclose path variables with double quotes to eliminate possible whitespace, you can drop the echo's seeing as you plan on running the script minimized. Last but not least, you do not need to specify full path to the user's desktop as %USERPROFILE% variable already exists as Drive:\Users\Username

Command Prompt Scripting

This is going to be hard to explain, I'll try my best.
I'm using a shell scripting (Test Executor) in command prompt to automate our regression testing. However i'm stuck on one thing.
Basically I have to copy this dynamically created folder in a location into another folder after the automated script is done (in CMD via scripting)
:: create an achieve directory
#SET ACHIEVE_DIR=C:\Test Result\LOG_Files
mkdir "%ACHIEVE_DIR%"
xcopy /y /s /e "%TEST_SOURCE_DIR%"\*.* "%ACHIEVE_DIR%"
This will copy all the files in the TEST_SOURCE_DIR, but i only want this dynamically created folder which changes according to the date stamp (BUT this date stamp is calculated by second - so i can't use that as a variable).
For example: After i run a test it'll create a folder under lets say C:\temp\ another folder named "TEST RUN - _2014-06-30 15_48_01_"
So if go into C:\temp i'll find a folder named "TEST RUN - _2014-06-30 15_48_01_" along with other folders. BUT i only want to copy this dynamic folder into Test Result\LOG_Files
What we are given is that the starting string will always be "TEST RUN -" but the ending string will interchange according to the time to the second (BUT we cannot go by that since it is the second of the starting test result).
I found out if i am in command prompt and i type in "cd TEST RU" and hit tab it'll finish it for me "cd TEST RUN - _2014-06-30 15_48_01_" Is there a way to script this tab hit or something?
Thanks!
This should work in a batch file to isolate the folder:
If there are a number of folders starting with test run - then tell us.
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do xcopy "%%a\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
This code looks for a test folder as discussed in the comments:
#echo off
SET "ACHIEVE_DIR=C:\Test Result\LOG_Files"
cd /d "c:\temp"
for /d %%a in ("test run -*") do (
pushd "%%a"
for /d /r %%b in ("test*") do (
xcopy "%%b\*.*" "%ACHIEVE_DIR%\" /s/h/e/k/f/c
)
popd
)
This batch file should do the job:
#echo off
rem Find the directory starting with "TEST RUN -" created last in C:\Temp.
set ACHIEVE_DIR=C:\Test Result\LOG_Files
for /F "usebackq delims=" %%D in ( `dir "C:\Temp\TEST RUN -*" /AD /B /O-D /TC` ) do (
set TEST_SOURCE_DIR=%%D
goto CopyDir
)
echo No directory starting with "TEST RUN -" found in C:\Temp.
pause
exit /b
:CopyDir
rem Copy all files and subdirectories from last test run to achieve
rem directory whereby xcopy automatically creates the achieve directory.
xcopy /E /I /Q /S /Y "%TEST_SOURCE_DIR%" "%ACHIEVE_DIR%"
set ACHIEVE_DIR=
set TEST_SOURCE_DIR=
Read the Microsoft pages about
dir
for
xcopy
for details about the used parameters of those 3 commands.
Or execute in a command prompt window the commands
dir /?
for /?
xcopy /?
one after the other to get the shorter help for those 3 commands output in the window.

BAT Command to Delete specific sub-folder

I have a follow bat command which will delete unnecessary files when i give respective file extension
#ECHO OFF
REM Change the path and the extension...
DEL /s /f /q "C:\Users\dell\Desktop\test\*.pdf"
DEL /s /f /q "C:\Users\dell\Desktop\test\*.csv"
ECHO Are you sure ? Press a key to continue.
PAUSE > NUL
ECHO Done
PAUSE
I am in need to insert specific sub-folder to delete along with files. Assume specific sub-folder name may be abc
Can any body help me on this how to insert "delete specific sub-folder"
Use the RD command to remove a (sub)directory. Since a directory may not be empty, you may want to add the /S switch to remove the entire directory tree. And since you are also going to use the command in a batch script, you may need the /Q switch as well, to avoid the confirmation prompt.
So add a command like this to your script:
RD /S /Q "C:\Users\dell\Desktop\test\Specific\Subfolder"

directory is not empty error

I try in my batch file o delete folder(BR) with many files and subdirectories, I try the following:
if exist C:\BR (
rmdir "C:\BR" /S /q
)
but sometimes I get an error that a specific folder is not empty.these folder contains files of CSS.
what the problem??
rd /s /q DIRNAME
rmdir /s /q DIRNAME
The files that you can't delete are in use.
Close whatever program is holding them open, probably your browser, and try again.
Let me guess, your trying to delete your %TMP% folder.
EDIT: To answer zipi's question.
It will delete every file and folder that it can. So, if c:\tmp\dir2\dir3\open.txt is open, c:\tmp\emptyDir is an empty directory, and you do this:
c:\>dir c:\tmp /b /s
c:\tmp\a.txt
c:\tmp\dir2\b.txt
c:\tmp\dir2\dir3\open.txt
c:\>rd /q /s c:\tmp
c:\>dir /s /b c:\tmp
c:\tmp\dir2\dir3\open.txt
You will have deleted:
c:\tmp\a.txt
c:\tmp\dir2\b.txt
And removed:
c:\tmp\emptyDir
But still have the directories...
c:\tmp
c:\tmp\dir2
c:\tmp\dir2\dir3
...an the file:
c:\tmp\dir2\dir3\open.txt
If instead, a.txt was open, you'd only have:
c:\tmp\
and
c:\tmp\a.txt
On win7 I use a simple bat file to go around the problem:
call :rmdir "my_directory_01"
call :rmdir "my_directory_02"
pause
goto :EOF
:rmdir
if exist %1 rmdir /s /q %1
if exist %1 goto :rmdir
goto :EOF
I had a similar problem. Tried lots of different solutions, but ultimately only the following worked:
rmdir c:\<directory> /s /q
Previously using other methods in CMD I was getting the following:
The directory is not empty.
I had the same issue and the solution is very silly. Please use /Q first and the /S to resolve your issue. so command should be something like:
IF EXIST %build_folder% RD /Q /S %build_folder%
Please let me know if this solves your issue.
Regards
Anuj
To remove directory in command line, you have to remove all of files and subsolders it cointainst at the first place. The problem may occur if some of those items are read-only. /f will try to force delete them.
Try
if exists C:\BR (del "C:\BR" /f /s /q)
Here you have MS docs of the DEL command: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/del.mspx?mfr=true
This worked for me
you will need to go to any drive where the folder is. Then right click on drive > properties > Check Scan disk or scan drive and for windows8 scan and repair
then go back to your folder and delete it
Batch - Getting "The directory is not empty" on rmdir command
In my case the failure to remove a directory using rd /Q /S and getting Directory not empty was down to file permissions. The batch job was doing a backup and removing oldest backup folder at the end to keep latest 10 backups. The normal user account only had read and execute permission on certain files in the sub folders. Running the batch file containing the rd commands under Task Scheduler with a tick in the option "run with highest privileges" enabled directory's to be removed.
You could achieve something similar as a one off if you run your batch file under cmd and choose run as administrator. In Windows 7 type CMD in the Search programs and files box, then right click on cmd.exe at the top of the popup window and click Run as Administrator. Then find and run your batch file in the cmd "black background" window.

Resources