Showing posts with label miscellaneous files operations. Show all posts
Showing posts with label miscellaneous files operations. Show all posts
Tuesday, September 3, 2013
Using PUSHD and POPD Commands in Batch Files
There are two really handy commands that are not very commonly used by the batch files' developers. They are named PUSHD and POPD. If you are familiar with Assembler, you may guess what these commands do. The first one stores the folder name (pushes directory) in some internal stack, and the second one brings it back (pops directory). Note that you can put several folders to the stack and them restore them in the reversed order (LIFO - last in, first out). It is much handier sometimes to use PUSHD/POPD instead of using special variables to store folders like in other programming languages.
Here's small example of using these commands:
ECHO Performing some file manipulations in one directory
REM Here are manipulations
REM Remembering this folder
PUSHD
CD AnotherFolder
ECHO Performing some file manipulations in another directory
REM Here are manipulations
REM Remembering this folder
PUSHD
CD YetAnotherFolder
ECHO Performing some file manipulations in this directory
REM Here are manipulations
REM Deleting temporary files in YetAnotherFolder
POPD
REM Deleting temporary files in AnotherFolder
POPD
REM Deleting temporary files in the first folder
REM That's all
So you should remember about these commands while you create batch files.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet.
Tuesday, August 6, 2013
Using Quotes in Batch Files
It is likely that using quotes is a hard topic for everyone who strats learning batch programming. We'll try to clarify this question a bit.
Quotes is just the way to pass the parameters containing spaces. Let's see an example:
DIR c:\Program Files
It seems that it's all right, and we'll list files and folders from Program Files directory. But this is wrong opinion because the space in batch files is delimiter for parameters passing to the batch command.To pass the single argument to the DIR command you should quote it:
DIR "c:\Program Files"
Quotes is not the part of the folder name. They only let the command know that space is the part of this name.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet.
Tuesday, July 2, 2013
Delete All Files Except Specified Ones
Sometimes it is neccessary to delete all files in a folder except some of them that are still useful. Here is small batch script that lets you delete all files except the specified ones. The parameter you pass to this script is the wildcard of files you want to leave.
@ECHO OFF
MD SAVE
XCOPY %1 SAVE > NUL
ECHO Y | DEL . > NUL
MOVE SAVE\*.* . > NUL
RD SAVE
ECHO Done
@ECHO OFF
MD SAVE
XCOPY %1 SAVE > NUL
ECHO Y | DEL . > NUL
MOVE SAVE\*.* . > NUL
RD SAVE
ECHO Done
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet.
Thursday, June 27, 2013
Batch File to Calculate Disk Space
If you need to calculate disk space used by a directory or by some files, the batch file below can be useful for you. If you call it with the parameter /d it lists each subdirectory. Without any parameters it just prints the total number of files and bytes.
@echo off
if not '%1=='/d goto howmuch1
dir /a-d /s|find "i"|find /v "Volume"|more
goto done
:howmuch1
dir /a-d /s|find "file(s)"|sort /r|find /n "f"|find "[1]"
@echo off
if not '%1=='/d goto howmuch1
dir /a-d /s|find "i"|find /v "Volume"|more
goto done
:howmuch1
dir /a-d /s|find "file(s)"|sort /r|find /n "f"|find "[1]"
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet.
Sunday, June 2, 2013
How to Delete Files from the Specified List (Batch Script)
There is quite a common task for batch file programming which is called 'Deleting Files from Specified List'. It is not hard to perform this operation. Here is a small batch script that may help you with this task:
SET LstName=
FOR /F %%i in ('type %LstName%') do DEL %%i
PAUSE
FOR /F %%i in ('type %LstName%') do DEL %%i
PAUSE
The only thing you need to do is specifying your own files list. What is this list? It is is simple text file with one name of file to be removed per one line. For example:
c:\Temp\*.*
d:\Dir\Data1.tmp
e:\Work\Temp\*.dat
etc.
We kindly remind that Dr.Batcher is the best program to create batch file, both .BAT and .CMD, try it if haven't tried it yet.
Friday, April 19, 2013
Remove Files Completely
It is necessary sometimes to remove files completely from a certain folder. It is handy to perform such action with the help of batch files.
Here you can find a small and useful script that might help you to clear the folder from all folders and files inside:
@echo off
echo ***Warning***
echo ***This Will Delete All Files and Folders Permanately!***
echo ***You Will Be Prompted To Confirm File Deletion...***
echo ***Verify All Files Before Deletion...***
del /P /S %1
pause
How to use this script? Save it (for example, as clearfiles.bat) and run from the command prompt with the folder to be cleaned as a parameter. For example:
clearfiles d:\Work\Old\Trash
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
Here you can find a small and useful script that might help you to clear the folder from all folders and files inside:
@echo off
echo ***Warning***
echo ***This Will Delete All Files and Folders Permanately!***
echo ***You Will Be Prompted To Confirm File Deletion...***
echo ***Verify All Files Before Deletion...***
del /P /S %1
pause
How to use this script? Save it (for example, as clearfiles.bat) and run from the command prompt with the folder to be cleaned as a parameter. For example:
clearfiles d:\Work\Old\Trash
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
Monday, March 18, 2013
How to Merge Two Files with Help of Batch Script
Imagine: you have some spiltted files and you want to merge them into the new file containing all of them. Of course, it is much simpler to perform this operation using a small batch script instead of copying data manually. Here you can see an example of this batch script (hope, it'll be quite understandable with the comments):
REM Advanced Files Merger
REM Adds files specified by mask to the end of the file passed as the first parameter
REM Masks or names of files to be added are passed as the second, the third, etc. parameters
REM E.g. usage is like: merger FileToBeExpanded.bin *.exe *.zip *.*
@ECHO off
IF '%1=='LooP GOTO %2
SET prgname=%0
SET destfile=%1
IF '%2==' GOTO done
IF not EXIST %2 GOTO done
:_loop
SHIFT
IF '%1==' GOTO done
FOR %%i in (%1) do CALL %prgname% LooP addfile %%i
GOTO _loop
:addfile
type %3>>%destfile%
echo.>>%destfile%
:done
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
REM Adds files specified by mask to the end of the file passed as the first parameter
REM Masks or names of files to be added are passed as the second, the third, etc. parameters
REM E.g. usage is like: merger FileToBeExpanded.bin *.exe *.zip *.*
@ECHO off
IF '%1=='LooP GOTO %2
SET prgname=%0
SET destfile=%1
IF '%2==' GOTO done
IF not EXIST %2 GOTO done
:_loop
SHIFT
IF '%1==' GOTO done
FOR %%i in (%1) do CALL %prgname% LooP addfile %%i
GOTO _loop
:addfile
type %3>>%destfile%
echo.>>%destfile%
:done
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
Tuesday, March 12, 2013
How to Find Desktop from Batch File
Sometimes it is necessary to operate with some files on desktop. It is easy to find a desktop itself :), but how to get the path to from a batch script? Here is a small bath script that might help you with this task.
ECHO chcp 1251 > 11.cmd
FOR /f "skip=4 tokens=2,*" %%i in ('reg query "HKCU\software\microsoft\windows\currentversion\explorer\shell folders" /v desktop') do ECHO set desktop=%%j >> 11.cmd
ECHO chcp 866 >> 11.cmd
CALL 11.cmd
ECHO %desktop%
As far as you can see, we retrieve the desktop location from Windows registry. The only trick is to process characters in different encodings, thus we need to create a temporary batch file.
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
ECHO chcp 1251 > 11.cmd
FOR /f "skip=4 tokens=2,*" %%i in ('reg query "HKCU\software\microsoft\windows\currentversion\explorer\shell folders" /v desktop') do ECHO set desktop=%%j >> 11.cmd
ECHO chcp 866 >> 11.cmd
CALL 11.cmd
ECHO %desktop%
As far as you can see, we retrieve the desktop location from Windows registry. The only trick is to process characters in different encodings, thus we need to create a temporary batch file.
We kindly remind that Dr.Batcher is the best program to create batch file, try it if haven't tried it yet.
Sunday, August 28, 2011
Sorting Web Pages Archive
The task is to sort the archive of different Web documents.
There is a plenty (about 5 thousands) of files named the following way:
number(3-6 digits)_name_.zip(jar,sis)
And the same amount of images in 'Screens' folder:
number(3-6 digits).jpg(gif,png)
Alse there are Web pages in 'Html' folder:
number(3-6 digits).htm(html)
Is it possible to write a batch script that creates folders named after a file and move there a file, a picture and a Web page? ZIP archive should be also extracted there.
The batch script to solve this task looks like this:
@Echo Off
Set ArcDir=c:\archives
Set ScreenDir=c:\screens
Set HTMLDir=c:\html
Set OutDir=C:\
For %%A In (%ArcDir%\*.zip %ArcDir%\*.jar %ArcDir%\*.sys) Do (
md "%%~nA">Nul 2>&1
If /I "%%~xA"==".zip" (
pkzip -extr=up "%%A" "%OutDir%\%%~nA">Nul 2>&1
) Else (
Copy /y "%%A" "%OutDir%\%%~nA">Nul 2>&1
)
Copy /y "%ScreenDir%\%%~nA.*" "%OutDir%\%%~nA">Nul 2>&1
Copy /y "%HTMLDir%\%%~nA.html" "%OutDir%\%%~nA">Nul 2>&1
)
Hope you'll find this example useful. You can modify this script with the help of our batch files editor called Dr.Batcher.
There is a plenty (about 5 thousands) of files named the following way:
number(3-6 digits)_name_.zip(jar,sis)
And the same amount of images in 'Screens' folder:
number(3-6 digits).jpg(gif,png)
Alse there are Web pages in 'Html' folder:
number(3-6 digits).htm(html)
Is it possible to write a batch script that creates folders named after a file and move there a file, a picture and a Web page? ZIP archive should be also extracted there.
The batch script to solve this task looks like this:
@Echo Off
Set ArcDir=c:\archives
Set ScreenDir=c:\screens
Set HTMLDir=c:\html
Set OutDir=C:\
For %%A In (%ArcDir%\*.zip %ArcDir%\*.jar %ArcDir%\*.sys) Do (
md "%%~nA">Nul 2>&1
If /I "%%~xA"==".zip" (
pkzip -extr=up "%%A" "%OutDir%\%%~nA">Nul 2>&1
) Else (
Copy /y "%%A" "%OutDir%\%%~nA">Nul 2>&1
)
Copy /y "%ScreenDir%\%%~nA.*" "%OutDir%\%%~nA">Nul 2>&1
Copy /y "%HTMLDir%\%%~nA.html" "%OutDir%\%%~nA">Nul 2>&1
)
Hope you'll find this example useful. You can modify this script with the help of our batch files editor called Dr.Batcher.
Sunday, August 21, 2011
Retrieving the List of Files Modified Lately
It's quite a common task to retrieve the list of files modified during the latest few days. It is easy to solve this task with the help of batch files:
@echo off
for %%i in (bmp jpg png gif) do (
forfiles -pC:\Test -s -m*.%%i -d+14 -c"cmd /c echo @PATH @FILE"
)
As far as you can see, this batch file searches for images in 'C:\Temp' folder, and shows those of them that were modified during the latest two weeks.
Hope you'll find this small example useful.
Monday, August 15, 2011
Clearing Sub-Folders in the Given Folder
Imagine: you have a shared folder where users can create there own sub-folders. Sometimes it is necessary to clean the whole shared folder, but the cleaning batch script has to leave all the sub-folders and just delete everything inside them. How to perform it? The small script below shows you quite an easy way to solve this problem. Hope you'll find it useful.
@echo off
pushd "U:\Share Folder"
for /d %%i in (*) do (
pushd "%%i"
rd /s /q "." 2>nul
popd
)
popd
@echo off
pushd "U:\Share Folder"
for /d %%i in (*) do (
pushd "%%i"
rd /s /q "." 2>nul
popd
)
popd
Monday, August 8, 2011
Search for a File on Local Drives
We all need sometimes to find a certain file on a local disk drive. It is not hard to implement search by name with the help of batch files. But what to do if you need to search for a file on all the drives available on a computer? The example script below shows how to perform it.
@echo off
setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\FileName.ext 2^>nul') do (
set "foundFile=%%a" & goto found
))
echo File not found
goto :EOF
:found
echo File found - %foundFile%
As far as you can see, we simply look through all letters for logical drives and then search for the needful file on the each drive separately.
@echo off
setlocal
for %%i in (c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /f "tokens=*" %%a in ('dir /b /s /a-d %%i:\FileName.ext 2^>nul') do (
set "foundFile=%%a" & goto found
))
echo File not found
goto :EOF
:found
echo File found - %foundFile%
As far as you can see, we simply look through all letters for logical drives and then search for the needful file on the each drive separately.
Thursday, July 14, 2011
Copy Files and Automatically Rename Existing Ones
Usually while copying large amount of files it is necessary to rename the existing files to avoid overwriting them with the copied ones. Usual commands like copy or xcopy don't let you perform renaming in automatic mode. Still you are able to create a batch script that will help you to solve this problem:for %%i in (SourceDir\*) do (
if exist "DestDir\%%~nxi" (
mv "%%i" "DestDir\~%%~nxi"
) else (
mv "%%i" "DestDir"
)
)
You can change this example script according to your needs with the help of our batch files editor. Hope you'll find this small example useful.
Thursday, June 23, 2011
Batch File to Create Virtual Disk Drive
Here is the small batch file that lets you create virtual disk drive. This drive doesn't really exist on your computer, it's something like a shortcut to the certain folder on your computer's HDD. The script will look like this:
net use X: \\komp\updat pass /user:user /persistent:yes >Nul 2<&1
set dir=T:\home
set od=X:\
del /Q /F %dir%\*.*
PING 1.1.1.1 -n 1 -w 2000 2>NUL | FIND "TTL=" >NUL
copy /Y %od%\*.* %dir%
As far as you can see, first of all we check for existence of a virtual drive with the physical letter. If this drive doesn't exist, we create it. Finally we copy some files on it.
net use X: \\komp\updat pass /user:user /persistent:yes >Nul 2<&1
set dir=T:\home
set od=X:\
del /Q /F %dir%\*.*
PING 1.1.1.1 -n 1 -w 2000 2>NUL | FIND "TTL=" >NUL
copy /Y %od%\*.* %dir%
As far as you can see, first of all we check for existence of a virtual drive with the physical letter. If this drive doesn't exist, we create it. Finally we copy some files on it.
Monday, June 13, 2011
Passing the Current Date to XCOPY
XCOPY is a really useful console command that allows you to make copies of files in a fast and easy way. One of the parameters of this command is '/d':
/d[:mm-dd-yyyy] : Copies source files changed on or after the specified date only. If you do not include a mm-dd-yyyy value, xcopy copies all Source files that are newer than existing Destination files. This command-line option allows you to update files that have changed.
If you need to pass the current date to this command, you'll be surprised: on some configurations of the operating system %date% returns the current date in dd.mm.yyy format, while XCOPY requires mm-dd-yyyy. What to do? Here is some batch code that helps you to solve this problem:
@echo off
setlocal
set "dd=%date:~0,2%"
set "mm=%date:~3,2%"
set "yy=%date:~-2%"
xcopy c:\*.txt c:\temp /d:%mm%-%dd%-%yy%
Saturday, June 4, 2011
Moving a File One Level Upper
We were asked via e-mail about moving files one level upper. Suppose this short example will be useful for everyone:
MOVE 1.ext ..\1.ext
Of course, the file to be moved and the batch script have to be placed in the same folder.
MOVE 1.ext ..\1.ext
Of course, the file to be moved and the batch script have to be placed in the same folder.
Sunday, May 15, 2011
Checking File Size in Batch Files
It's sometimes necessary to check the size of a given file. For example, if you are to delete too big files on your hard drive or split single file into many parts. As usual, batch files are powerful enough to perform such task. The example below shows how to check batch file size (file is called 1.txt) and exit the script if the size is equal to 100 Kb.for %%a in (1.txt) do if %%~za EQU 102400 goto :EOF
You can use this example in scripts that you create with help of our award-winning program designed to write batch files.
You can use this example in scripts that you create with help of our award-winning program designed to write batch files.
Subscribe to:
Posts (Atom)


