so i found this nice batch for window that would compress every file of the same extension in the same directory into bzip2 by dragging and dropping any of the files into it but i would like to take it further and make it that when i drag and drop a folder into it. it would compress all the files in it, including the sub-folders until it reaches the end. obviously i would guess it has something to do with looping with using %%d but i could not exactly figure it out.
#echo off
if [%1]==[] goto usage
for /f %%i in ("%1") do (
echo %%~di
echo %%~pi
echo %%~xi
set rootpath="%%~di%%~pi*%%~xi"
)
for %%f in (%rootpath%) do (
"C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9
del "%%f" /s /f /q
)
echo Finished operations!
goto exit
:usage
echo You have to drag and drop a file on this batch script!
echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
echo The only thing you really need is to change the path to your 7-Zip installation
echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically
:exit
pause
I share with you this helpful and well commented batch script posted by enteleform on superuser
I just modified this variable Set archivePath="%%~x.zip" to Set archivePath="%%~x.bz2"
How to make 7-zip do a whole bunch of folders
#Echo OFF
SetLocal EnableDelayedExpansion
Rem // 7-Zip Executable Path
Set sevenZip="C:\Program Files\7-Zip\7z.exe"
Rem // START: NewLine Variable Hack
Set newLine=^
Rem // END: NewLine Variable Hack !! DO NOT DELETE 2 EMPTY LINES ABOVE !!
Rem // Set ErrorLog Variables
Set errorCount=0
Set separator=--------------------------------------------------------
Set errorLog=!newLine!!newLine!!separator!!newLine!!newLine!
Set errorPrefix=ERROR #:
Set successMessage=All Files Were Successfully Archived
Rem // Loop Through Each Argument
SetLocal DisableDelayedExpansion
for %%x in (%*) do (
Rem // Use Current Argument To set File, Folder, & Archive Paths
SetLocal DisableDelayedExpansion
Set filePath="%%~x"
Set directoryFiles="%%~x\*"
Set archivePath="%%~x.bz2"
SetLocal EnableDelayedExpansion
Rem // Source Is A Folder
if exist !directoryFiles! (
Set sourcePath=!directoryFiles!
)
Rem // Source Is A File
if not exist !directoryFiles! (
Set sourcePath=!filePath!
)
Rem // Print Separator To Divide 7-Zip Output
echo !newLine!!newLine!!separator!!newLine!!newLine!
Rem // Add Files To Zip Archive
!sevenZip! A -TZIP !archivePath! !sourcePath!
Rem // Log Errors
if ErrorLevel 1 (
Set /A errorCount=errorCount+1
Set errorLog=!errorLog!!newLine!!errorPrefix!!sourcePath!
)
)
Rem // Print ErrorLog
if !errorCount!==0 (
Set errorLog=!errorLog!!newLine!!successMessage!
)
Echo !errorLog!!newLine!!newLine!!newLine!
Rem // Keep Window Open To View ErrorLog
pause
You could probably do that with a single line batch-file:
#For %%G In ("%~1")Do #If "%%~aG" GEq "d" (For /F Delims^= %%H In ('""%__AppDir__%where.exe" /R "%%~G" * 2>NUL|"%__AppDir__%findstr.exe" /EVIL ".bz2""')Do #"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1)&"%__AppDir__%timeout.exe" /T 3
If you'd like it over multiple lines for readability:
#For %%G In ("%~1") Do #If "%%~aG" GEq "d" (
For /F "Delims=" %%H In (
'""%__AppDir__%where.exe" /R "%%~G" * 2>NUL | "%__AppDir__%findstr.exe" /EVIL ".bz2""'
) Do #"%ProgramFiles%\7-Zip\7z.exe" a -tbzip2 "%%~dpH%%~nH.bz2" "%%H" -mx9 -sdel -w >NUL 2>&1
"%__AppDir__%timeout.exe" /T 3
)
These examples should only work if you drag and drop a directory onto it, or call it on the command line with a directory as the first argument.
#echo off
for /f %%i in ("%1") do (
echo %%~di
echo %%~pi
echo %%~xi
set rootpath="%%~di%%~pi*%%~xi"
)
for /R %%f in (*) do (
"C:\Program Files\7-Zip\7z.exe" a -tbzip2 "%%f.bz2" "%%f" -mx9 -x!"packall.bat"
del "%%f" /s /f /q
)
echo Finished operations!
goto exit
:usage
echo You have to drag and drop a file on this batch script!
echo Sorry for the poor documentation, but if you'll want to use it, you have to edit the .bat file
echo The only thing you really need is to change the path to your 7-Zip installation
echo Then simply drag and drop a file in a folder you want to BZip2, and it'll do the rest automatically
:exit
pause
Cheers to my friend Anya who found a solution, so the way it would work with the script above is that you make a batch file name it packall.bat
save it anywhere as it will delete itself at the end of the process.
when you want to compress bunch of files into bz2 you copy it and put it in inside a folder made with any name in your desktop.
make sure its name has no spaces nor its sub-folders as that may confuse batch and make it compress your desktop contents for some reason.
click on the batch, then it will compress all the files within the same folder its in and their sub-folders and automatically delete itself.
Video example:
http://billstuff.site.nfoservers.com/e79nwk69.mp4
IMPORTANT NOTE for some reason if there duplicate names of files with the same extension at sub-folders they will be deleted
Don't forget the folder and its sub-folder names should not have a space
Best of luck!
Related
I have to manipulate dozens if not hundreds of images fairly often. When I get the images, it's often 1018u182012480d1j80.jpg type filenames, so I can select all of the images, and then hit 'F2' to rename them all to image (1).jpg | image (2).jpg | ... etc. Can someone write me a batch script to remove the parentheses and the spaces in the file names?
If possible, it'd be great if I didn't even have to open it up and edit the batch file to update the current dir, I.E, if it can just use %CD% or something to get the current folder, that would be awesome.
I don't understand batch files at all but here's one I found that will remove the parentheses, but I have to add the sourcedir.
#ECHO OFF
SETLOCAL
SET "sourcedir=C:\.." #Can we make this just find the location?
FOR /f "delims=" %%a IN (
'dir /b /s /a-d "%sourcedir%\*" '
) DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
SET "newname=!name:)=!"
SET "newname=!newname:(=!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (ECHO cannot RENAME %%a
) ELSE (REN "%%a" "!newname!%%~xa")
)
endlocal
)
The script should work if file names/paths does not contain any ! exclamation mark.
Supply a folder path as an argument (see sample calls below). This can be done even by file explorer's drag&drop feature (drag a folder, drop on batch). Read call /? or Command Line arguments (Parameters) for %~1 explanation.
Narrow to only .jpg files using "%sourcedir%\*.jpg" in FOR /f line.
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
SET "sourcedir=%~1" supplied string
if NOT defined sourcedir SET "sourcedir=%CD%" current directory if nothing supplied
rem basic validity check
if NOT exist "%sourcedir%\*" (
echo wrong directory "%sourcedir%"
goto :next
)
FOR /f "delims=" %%a IN ('dir /b /s /a-d "%sourcedir%\*" ') DO (
SET "name=%%~na"
SETLOCAL ENABLEDELAYEDEXPANSION
rem remove ) right parenthesis
SET "newname=!name:)=!"
rem remove ( left parenthesis
SET "newname=!newname:(=!"
rem remove ↓ space(s)
SET "newname=!newname: =!"
IF "!name!" neq "!newname!" (
IF EXIST "%%~dpa!newname!%%~xa" (
ECHO cannot RENAME %%a
) ELSE (
rem operational RENAME command is merely ECHO-ed for debugging purposes
ECHO RENAME "%%a" "!newname!%%~xa"
)
)
ENDLOCAL
)
:next
rem pause to see output
pause
Sample call without argument - applies to the current directory:
d:\bat\SU> D:\bat\SO\43097467.bat
RENAME "d:\bat\SU\New Text Document.txt" "NewTextDocument.txt"
RENAME "d:\bat\SU\Files\ruzna pisma.png" "ruznapisma.png"
RENAME "d:\bat\SU\Files\volume control options.png" "volumecontroloptions.png"
RENAME "d:\bat\SU\Files\volume mixer.png" "volumemixer.png"
RENAME "d:\bat\SU\Files\1126981\ERKS 100004_thumb.jpg" "ERKS100004_thumb.jpg"
Press any key to continue . . .
Sample call with argument - applies to the supplied directory:
d:\bat\SU> D:\bat\SO\43097467.bat D:\test\43097467
RENAME "D:\test\43097467\image do - Copy (2).bmp" "imagedo-Copy2.bmp"
cannot RENAME D:\test\43097467\image do - Copy (3).bmp
RENAME "D:\test\43097467\image do - Copy (4).bmp" "imagedo-Copy4.bmp"
Press any key to continue . . .
Sample call, argument with spaces must be enclosed in a pair of double quotes:
d:\bat\SU> D:\bat\SO\43097467.bat "D:\bat\odds and ends\a b"
RENAME "D:\bat\odds and ends\a b\c d\my list_Utf8.txt" "mylist_Utf8.txt"
RENAME "D:\bat\odds and ends\a b\c d\e f\h i batch.bat" "hibatch.bat"
RENAME "D:\bat\odds and ends\a b\c d\e f\System locale.png" "Systemlocale.png"
Press any key to continue . . .
d:\bat\SU>
How could i rename a certain part of all files inside a directory.
Example
[HorribleSubs] File1 [720p].mkv
[HorribleSubs] File2 [1080p].mkv
Must be renamed into
File1.mkv
File2.mkv
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
FOR /f "tokens=2delims=[]" %%a IN (
'dir /b /a-d "%sourcedir%\[*]*[*].mkv" '
) DO (
SET "newname=%%a"
ECHO(REN "%sourcedir%\[*]%%a[*].mkv" "!newname:~1,-1!.mkv"
)
GOTO :EOF
This should fix your problem.
The required REN commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO(REN to REN to actually rename the files.
This is a simple batch you can use to rename a single file manually.
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
pause
this has a loop to do multiple files manually
#echo off
title Rename Bat
echo This bat must be in the folder that
echo contains the files to be renamed.
:begin
echo Enter File Name
set /p old=
echo Enter New Name
set /p new=
ren "%old%" "%new%"
echo Done
ping -n 3 127.0.0.1 >NUL
goto begin
its real simple hope it helps. fyi it works on windows XP
place file In directory you wanna rename
#echo off
setlocal enabledelayedexpansion
dir /b ¦ find /v /c "*">filescount.txt
for /f %%f in (filescount.txt) do (
set /a "nof=%%f-2"
for /L %%n in (1 1 !nof!) do (
for %%a in (*.mkv) do (
rename "%%a" "file%%n%%~xa"
)
)
)
i'm willing to have a windows batch .bat script that hide files by renaming them and log the renaming into a file to undo it later.
privacy : sometime i don't want other people using my PC to see some of my files as they might be considered as personnal (yeah PC is for Personal Computer), i need to hide them from public view.
3 options:
-use a full featured encryption SW to encrypt the file & folder i want to hide -- 100% efficient but way too slow and heavy when processing lots of files...
-play with a zipper as 7z and compress/decompress with a password and 256bits AES -- efficient but very long too and not easy in case of modifying the file content...
-OR simply have the name of files fully renamed : no explicit name, a standard random computer generated name, so no one would guess what is inside and plainly think it's a system file. (And of course no extension!! .mp3 .avi .txt .xls etc...) This way file are still accessible if you know (i mean I KNOW) how to open them. On a windows OS that hold 130000+ files, who will find my secrets files? (hope no one will ;-) )
So i need a .bat script that rename all theses files into that and undo it.
The script might be able to go recursively through folder and rename folders as well...
expected behavior is like this :
hide_my_folders.bat folder_path
expected before/after is like this :
secret_folder1* ---> 124-235-4443-4546*
secret_folder2* ---> 5461-659-3-14*
my_secret_agenda.txt ---> 156-1658-3-44
my_secret_picture.jpg ---> 16971-2326-11-1
my_secret_recording.mp3 ---> 1234-1445-788-99
*folders stays folders but all names are scrambled
To get started, i already have a .bat script that rename everything in the hard-coded folder d:\2sort\ (no recursion)
so i need some help to add :
-the recursion (hard coded or may be as an option on the command line "/s"...)
-parameter input (pass the folder as parameter)
note the : SET TranslationFile=5173-19632-45713-132
as it is the file containing the original name list to reverse the renaming
#ECHO OFF
REM Randomly renames every file in a directory.
SETLOCAL EnableExtensions EnableDelayedExpansion
REM HARD SETUP INTO d:\2sort\
d:
cd d:\2sort\
REM HARDCODED TRANSLATION FILE WITH EXPLICIT NAME VIOLATION AS 45713 > 2^15=32768
SET TranslationFile=5173-19632-45713-132
IF NOT EXIST %TranslationFile% (
ECHO Rename files mode %CD%
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO Aborting.
GOTO :EOF
)
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
SET Use=.
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!-!RANDOM!!Use!
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
)
)
)
) ELSE (
ECHO Undo rename mode %CD%
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
)
& thanks
#echo off
setlocal
SET root_dir=%~1
SET root_dir=d:\2test
REM -------------------------------------------------------
REM -- PUSHD all directories tree --
SET /A COUNT=1
FOR /D /R %root_dir% %%G in (*) DO (
Pushd "%%G"
SET /A COUNT+=1
)
REM -------------------------------------------------------
REM -- POPD all directories to process them in reverse order ( \d1\d1.1 must be processed before \d1) --
:while
IF %COUNT% gtr 1 (
REM -- PROCESS CURRENT FOLDER --
CALL :process_folder
popd
SET /A COUNT-=1
GOTO :while
)
REM -- PROCESS ROOT FOLDER --
CD %root_dir%
CALL :process_folder
REM -------------------------------------------------------
REM -- ALL DONE --> now exit --
GOTO :EOF
REM -------------------------------------------------------
REM PROCESS THE DIRECTORY
:process_folder
pwd
SETLOCAL EnableExtensions EnableDelayedExpansion
REM HARDCODED TRANSLATION FILE WITH EXPLICIT NAME VIOLATION AS 45713 > 2^15=32768
SET TranslationFile=5173-19632-45713-132
IF NOT EXIST %TranslationFile% (
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
SET Use=.
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!-!RANDOM!!Use!
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
)
)
)
) ELSE (
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
)
:EOF
output is run twice (do/undo)
D:\>test
d:\2test\d2\d2.1
d:\2test\d1\d1.1\d1.1.2
d:\2test\d1\d1.1\d1.1.1
d:\2test\d1\d1.1
d:\2test\d2
d:\2test\d1
Access is denied.
d:\2test
Access is denied.
Access is denied.
D:\>test
d:\2test\d2\16516-17860-21197-21380
d:\2test\d1\d1.1\9568-2224-16391-19037
d:\2test\d1\d1.1\23997-552-22581-12114
d:\2test\d1\d1.1
d:\2test\d2
d:\2test\d1
The system cannot find the file specified.
d:\2test
The system cannot find the file specified.
The system cannot find the file specified.
D:\>
almost good, i have an unfortunate and nasty access denied from windows... so some folders cannot be renamed!
anyone to help fix it?
note: hardcoded in "d:\2test\" for testing
thanks
I would like to have a batch file wich will sort files by file type and sorts them into folder.
For example I will run this batch file in some folder and .PDF files will be saved in "PDF" folder, same to do with other file types. Is possible to do that in command line?
Thank you.
Please put the code below in a .bat file and save it to your folder with files and run it.
#echo off
rem For each file in your folder
for %%a in (".\*") do (
rem check if the file has an extension and if it is not our script
if "%%~xa" NEQ "" if "%%~dpnxa" NEQ "%~dpnx0" (
rem check if extension forlder exists, if not it is created
if not exist "%%~xa" mkdir "%%~xa"
rem Copy (or change to move) the file to directory
copy "%%a" "%%~dpa%%~xa\"
)
)
Try this:
#echo off
setlocal enabledelayedexpansion
for %%a in (*.*) do (
set "fol=%%~xa" & set "fol=!fol:.=!"
if not exist !fol! md !fol!
for /f %%b in ('dir /on /b *.*') do (
if %%~xb EQU .!fol! move %%~nxb !fol!
)
)
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET destdir=c:\destdir
SET "extdone=:"
FOR /f "delims=" %%a IN ('dir /b /a-d') DO (
SET ext=%%~xa
IF DEFINED ext (
SET extdone|FIND /i ":%%~xa:" >NUL
IF ERRORLEVEL 1 (
SET extdone=:%%~xa: !extdone!
IF EXIST "%destdir%\!ext:~1!" (
ECHO MOVE "*%%~xa" "%destdir%\!ext:~1!"
) ELSE (ECHO no MOVE "%%~xa")
)
)
)
GOTO :EOF
This batch should do as you ask.
The required commands are merely ECHOed for testing purposes. After you've verified that the commands are correct, change ECHO MOVE to MOVE to actually move the files.
I've assumed that you only wish to move the files if destinationdirectory\extensionfound exists. If you want to create a directory for a new extension, simply add a line
md "%destdir%\!ext:~1!" 2>nul
after the SET extdone=:%%~xa: !extdone! line.
the Batch File should scan a certain folder continuously for new csv files and whenever a new csv-file is placed in the folder, the csv-file should be renamed to a specific filename, because a firefox extension shall read that file.
I have already written a batch file for this but I think the solution is not perfect.
This is my attempt:
I start with the batch-file InitRun.bat:
#echo off
for /L %%i in (1,1,86400) do (
call FFRun1.bat
timeout /T 1
)
FFRun1.bat looks like this:
#echo off
FOR %%f in (*Data.csv) do (
echo %%f
set FILE=%%f
call :copy
)
goto end
:copy
copy /y %FILE% merged.csv
del %FILE%
call :RunFF
:RunFF
type merged.csv
pushd "C:\Program Files (x86)\Mozilla Firefox\"
start /wait firefox.exe
popd
:end
exit /b
If I place two or more csv-files at the same in the Folder, my solution can only process one csv and merged.csv only contains the last processed csv. In addition Firefox opens several windows, but also only works with the last csv which was copied to merged.csv.
Each new csv-File has the filename *Data.csv and should be renamed to merged.csv and for each new csv-file, firefox should start in a new tab and process the current merged.csv.
The csv-Files should not be renamed all at once, but one after another. Also Firefox should not start multiple windows/instances at a time.
I hope you can help me.
Regards,
Kepler
It should do what you indicate iterating over the directory in date order and converting the *data.csv into merge csv for later process (firefox changed with notepad for testing, adapt as neede). Also it will check if .csv files are in use, and wait for file to become available. If a file can not be processed (in use) no later file will be accesible until locked file become available (process files in generation order)
#echo off
setlocal enableextensions
set "folder=d:\temp"
set "files=*data.csv"
pushd "%folder%"
for /l %%a in (0 0 1) do (
if exist "%files%" (
echo files found
set "done="
for /f "tokens=*" %%d in ('dir /b /od "%files%"') do if not defined done (
call :replace "merged.csv" "%%d"
if not errorlevel 1 (
start "" /wait notepad "merged.csv"
) else (
set "done=1"
)
)
) else (
echo no files to process
)
timeout.exe /t 2 >nul
)
endlocal
exit /b
:replace target source
if exist "%~1" del /f /q "%~1" >nul 2>nul
if errorlevel 1 (
echo file "%~1" is in use
exit /b 1
)
ren "%~2" "%~1" >nul 2>nul
if errorlevel 1 (
echo file "%~2" is in use
exit /b 1
)
exit /b 0