I have a .batch script in my Send to folder on Windows which I use to compress multiple files in the folder to a .zip. The problem I'm having is, it's including the extension in the archive file name.
I've search on google, read a lot of posts on stackexchange etc and none of the solutions posted seem to work for me.
#echo off
for %%A in (*.*) do call :doit "%%A"
goto end
:doit
if "%~x1"==".bat" goto :eof
if "%~x1"==".7z" goto :eof
if "%~x1"==".zip" goto :eof
if "%~x1"==".rar" goto :eof
"C:\Program Files\7-Zip\7z.exe" a -tzip %1.zip %1 -sdel
goto :eof
:end
Myfile.txt -> MyFile.txt.zip
I want to remove the .txt from file name if possible.
You can use %~n1 which uses the filename only of the first argument. Here is a possible solution:
#echo off
for %%A in (*.*) do (call:doit "%%A")
goto end
:doit
if "%~x1" NEQ ".bat" (
if "%~x1" NEQ ".7z" (
if "%~x1" NEQ ".zip" (
if "%~x1" NEQ ".rar" (
"C:\Program Files\7-Zip\7z.exe" a -tzip %~n1.zip %~n1 -sdel
:end
exit /b 0
I'm not sure of the syntax for your compressor program, because the answer you've accepted looks wrong both in the fact spaces aren't protected and %~n1 may be missing it's extension.
You could probably do this as a single line:
#For /F "Delims=" %%A In ('Dir /B/A-D-S-L "%~1"^|FindStr /IVE "\.7z \.bat \.rar \.zip"') Do #"%ProgramFiles%\7-Zip\7z.exe" a -tzip "%%~nA.zip" "%%~A" -sdel
Related
Want to make a batch file from a folder of 30,000 files from let's say C:\Users\NAME\Desktop, that will create a folder named "1", put 500 files in, no matter the name of the file, and then loop naming the next "2", until all 30,000 are in folders of 500 files each.
Thanks in advance for the help :)
#ECHO OFF
SETLOCAL
rem The following settings for the source directory and destination directory are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:\your files\t w o"
SET "destdir=u:\your results"
PUSHD "%sourcedir%"
ECHO sd=!sourcedir!
FOR /f "tokens=1*delims=:" %%b IN (
'dir /b /a-d * png *.doc^|findstr /n "."'
) DO (
SET /a subdir=1+(%%b / 7^)
FOR /f "tokens=2delims==" %%e IN ('set subdir') DO (
ECHO MD "%destdir%\%%e" 2>NUL
ECHO MOVE "%%c" "%destdir%\%%e\"
)
)
popd
GOTO :EOF
Always verify against a test directory before applying to real data.
I used a limit of 7 for testing. Modify as you will
Required md and move commands are merely echoed for verification. remove the echoes to activate after testing.
Found this from Compo, sorry didn't reference it!
#Echo Off
If /I Not "%__CD__%"=="%~dp0" PushD "%~dp0" 2>Nul||Exit/B
SetLocal EnableDelayedExpansion
Set "DirN=-1"
:Check_DirN
Set/A "DirN+=1"
If Exist "%DirN%" GoTo Check_DirN
Set "limit=2"
For %%A In (*.png *.doc) Do (
If Not Exist "%DirN%" MD "%DirN%"
If /I Not "%%~nxA"=="%~nx0" RoboCopy . "%DirN%" "%%A" /MOV 1>NUL
Set/A "limit-=1"
If !limit! Lss 0 GoTo Check_DirN
)
Echo(Task Done!
Timeout -1 1>Nul
I am writing a batch file to read all the files within the folder.
Below is my code:
#echo off
setlocal EnableDelayedExpansion
for %%I in (C:\test\*.print_job.*) do (
Set Name=%%~nxI
echo !Name!
)
pause
I am able to get all the .print_job files but now I want to read all the files and look for a specific identifier.
if the file contains "MOUNT" then move that file to C:\Folder1
if the file contains "PROD" then the file should get moved to
C:\Folder2
if the file contains "SPI" then the file should get moved to
C:\Folder3
Thanks in advance
#echo off
rem string target destination
call :movefiles "MOUNT" "C:\test\*.print_job.*" "C:\Folder1"
call :movefiles "PROD" "C:\test\*.print_job.*" "C:\Folder2"
call :movefiles "SPI" "C:\test\*.print_job.*" "C:\Folder3"
goto :eof
:movefiles
if not exist "%~3" md "%~3"
for /f "delims=" %%A in ('2^>nul findstr /l /m /c:"%~1" "%~2"') do (
move "%%~A" "%~3"
)
goto :eof
Use of call :movefiles to handle each of the 3 strings to
search for in the target files.
Call syntax: call :movefiles <string> <target> <destination>
Makes the destination directory if not exist. If string found
in a target file, the file will be moved into the destination
folder.
The findstr arguments used are:
/l Uses search strings literally.
/m Prints only the filename if a file contains a match.
/c:string Uses specified string as a literal search string.
You can insert rd "%~3" after the for loop if you want to
remove empty destination folders.
To loop every 2 seconds:
#echo off
:main
rem string target destination
call :movefiles "MOUNT" "C:\test\*.print_job.*" "C:\Folder1"
call :movefiles "PROD" "C:\test\*.print_job.*" "C:\Folder2"
call :movefiles "SPI" "C:\test\*.print_job.*" "C:\Folder3"
timeout /t 2 /nobreak >nul
goto :main
:movefiles
if not exist "%~3" md "%~3"
for /f "delims=" %%A in ('2^>nul findstr /l /m /c:"%~1" "%~2"') do (
echo move "%%~A" "%~3"
)
goto :eof
You may need to use Ctrl+C to end the script as it is in a continuous loop.
If you can use a task scheduler instead then that could work.
If a file name with the search word removed is different it had been in there.
#echo off
for %%I in (C:\test\*) do Call :Sub "%%I"
Pause
Goto :Eof
:Sub
Set "Name=%~nx1"
if "%Name%" neq "%Name:MOUNT=%" (move "%~1" "C:\Folder1\" & Goto :Eof)
if "%Name%" neq "%Name:PROD=%" (move "%~1" "C:\Folder2\" & Goto :Eof)
if "%Name%" neq "%Name:SPI=%" (move "%~1" "C:\Folder3\" & Goto :Eof)
I got this far into making a code (with you guys help on here) but it doesn't do what it's supposed to do..
The goal of the batch is to open all K_E*.dwg files and execute a script on those.
The length of the K_E*.dwg files should be 9. Example K_E123456.dwg.
So far, this code opens a dos-window and closes it again..
my folders structure is like this :
C:\Users\b00m49\Desktop\LSPTEST\0031\00\K_E000031.dwg
C:\Users\b00m49\Desktop\LSPTEST\0031\01\K_E010031.dwg
C:\Users\b00m49\Desktop\LSPTEST\0032\00\K_E000032.dwg
C:\Users\b00m49\Desktop\LSPTEST\0033\00\K_E000033.dwg
...
any help into running this code would be great..
I am very new to batch, so explanation about steps may be handy and might allow me to solve small problems.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
FOR /f "tokens=1,9delims=\" %%m IN ("%%a") DO (
REM %%n contain dirname or filename or empty
REM %%n is only empty for 0, 1 or 2 levels down.
IF "%%n"=="" (
SET "name=%%~na"
IF "!name:~9!"=="" IF "!name:~8!" neq "" (
REM the name is not longer than 9 characters, but IS longer than 8
ECHO(start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
)
)
)
)
GOTO :EOF
You may use this method instead, that offer a better identification of the desired file names:
#echo off
setlocal
set "sourcedir=C:\Users\b00m49\Desktop\LSPTEST"
cd "%sourcedir%"
for /F "delims=" %%a in ('dir /S /B K_E*.dwg ^| findstr "\\K_E[0-9][0-9][0-9][0-9][0-9][0-9]\.dwg"') do (
ECHO start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr"
)
The for /r "%sourcedir%" %%a command is replaced by a cd "%sourcedir%" and a posterior dir /S command; this method process the same files: all files at any level inside the source dir. The list of files is passed via a | pipe into findstr filter command, that allows to pass just those names that have "\K_E"+6 digits+".dwg" precisely. For further details, type findstr /?. The output is taken by the for /F "delims=" %%a command.
PS - Why you use start /wait command? its net effect is the same as execute the acad.exe command alone...
I'm new to this website, and didn't notice that someone edited his comment into something that works..
I don't completely understand how, but here's the code.
#ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\Users\b00m49\Desktop\LSPTEST"
for /r "%sourcedir%" %%a in (K_E*.dwg) do (
FOR /f "tokens=1,9delims=\" %%m IN ("%%a") DO (
REM %%n contain dirname or filename or empty
REM %%n is only empty for 0, 1 or 2 levels down.
IF "%%n"=="" (
SET "name=%%~na"
IF "!name:~9!"=="" IF "!name:~8!" neq "" (
REM the name is not longer than 9 characters, but IS longer than 8
ECHO(start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
)
)
)
)
pause
GOTO :EOF
Change
ECHO(start /wait "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
to
start /wait "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
or to
start "" "C:\Program Files\Autodesk\Autocad 2013\acad.exe" "%%a" /b "C:\Users\b00m49\Desktop\LSPTEST\expSQM.scr")
which should run all of the conversions in parallel.
I am writing an automated backup program that can be scheduled with Task Scheduler.
I've got it done to the point where it will successfully make sure that it can read/write both the source and destination folders, then copy everything from the source to the destination, ignoring files that haven't been changed. What I want to do is "sync" the destination folder to the source folder. I've tried making it echo the filenames from %destdir% (variable for the inputted destination path) into a .txt file, then compare the .txt file to the filenames in %sourcedir% (variable for the inputted source path), deleting anything that isn't listed in the .txt file. This works on paper, but I think my syntax may be wrong, as I'm not entirely familiar with if and for. This is what the backup routine looks like so far:
echo Copying files...
xcopy /s/e /y /h /k /z /d /i %sourcedir% %destdir%
for /r "%destdir%" %%F in (*.*) do echo %%F>> list.txt
for /F "tokens=2* delims=\" %%I in (list.txt) do if not exist "%sourcedir%" del "%destdir%"
del list.txt
pause
if %errorlevel%==0 goto success
if %errorlevel%==gtr 0 goto failure
I must be doing something wrong here (my guess is for /F "tokens=2* delims=\" %%I in (list.txt) do if not exist "%sourcedir%" del "%destdir%". Something about that doesn't seem right, but I can't quite put my finger on it. I've been debugging for a good hour or so now, and I can't seem to fix this problem.
Any help would be appreciated.
ECHO Copying files...
SET "SOURCEDIR=Dir to copy"
SET "DESTDIR=Dir where the files shoud be"
XCOPY /s/e /y /h /k /z /d /i "%SOURCEDIR%" "%DESTDIR%"
FOR /R "%DESTDIR%" %%F IN (*.*) DO ( ECHO %%F>> list.txt
FOR /F "tokens=2* delims=\" %%I in (list.txt) DO IF NOT EXIST "%SOURCEDIR%" DEL "%DESTDIR%"
DEL list.txt
IF "%errorlevel%" EQU "0" ( goto :SUCCESS )
IF "%errorlevel%" GTR "0" ( goto :FAILURE )
:SUCCESS
<<command you want to use here.>>
GOTO :END
:FAILURE
<<command you want to use here.>>
GOTO :END
:END
EXIT /B "%ERRORLEVE%"
I have a ZIP archive with a deep directory structure. I want to unpack just the last directory ("Package"). And I want all of it's sub-directories and content. I don't know exactly what will be in there, but I want it all.
Let's say the archive looks like this
package.zip
folder1\
folder2\
folder3\
Package\
PackageTmp\
I started writing this script using the 7zip CLI in a Windows batch file.
#ECHO OFF
FOR /F "usebackq tokens=*" %%i in (DIR *.zip /B /A:-D) DO CALL :EXTRACT %%i
GOTO :EOF
:EXTRACT
IF "%1"=="" GOTO :EOF
SET zipfile=%1
SET folder=%zipfile:~0,-4%
7za.exe x %zipfile% -o%folder% -y PackageTmp*
GOTO :EOF
7zip always tells me: No files to process.
You have to add the -r unzip option and also add the backticks to the for loop, see below.
FOR /F "usebackq tokens=*" %%i in (`DIR *.zip /B /A:-D`) DO CALL :EXTRACT %%i
GOTO :EOF
:EXTRACT
IF "%1"=="" GOTO :EOF
SET zipfile=%1
SET folder=%zipfile:~0,-4%
7za.exe x %zipfile% -o%folder% -y -r PackageTmp*
GOTO :EOF
Backticks are used to execute the command in between them. You can only use them if you add usebackq in the options of the for loop.
Executing a command and looping over the results can also be done in the following (more old-fashioned and limited) way with single quotes.
FOR /F "tokens=*" %%i in ('DIR *.zip /B /A:-D') DO CALL :EXTRACT %%i
See dos command for /? for more info.