Batch or Powershell Script to zip specific files [closed] - vba

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have the following folder structure:
-Videos
-1. Summer
summer.mp4
summer.srt
summer2.zip
-2. Winter
winter.mkv
winter.vtt
- ..
How can I create a batch or powershell script that results in the following folder structure:
-Videos
-1. Summer
summer.mp4
1. Summer.7z
-2. Winter
winter.mkv
2. Winter.7z
- ..
So basically iterate through all sub-folders, zip all the contents except video formats using 7zip and delete the original files that were zipped. I'm open to suggestions using VB!

After 2 weeks of research I managed to do it myself. Here's the answer for anyone with a similar situation:
#echo off
cls
set zip="C:\Program Files (x86)\7-Zip\7z.exe"
set loc=C:\User\Downloads\Videos\UnConverted
cd %loc%
for /d %%D in (%loc%\*) do (
for /d %%I in ("%%D\*") do (
cd %%I
%zip% a -t7z -mx9 "%%~nxI.7z" *.txt *.html *.vtt *.srt *.zip *.jpeg *.png
del /S *.txt *.html *.vtt *.srt *.zip *.jpeg *.png
)
)
cd %loc%
echo ----- Compression Complete! -----
pause
The first for loop iterates through the every subfolder of the root folder while the second for loop iterates through each subfolder of the root folders subfolders. After that
cd %%I - To enter the sub-sub-folder
%zip% - To call the 7z cli
a - add files to archive
t7z - using the .7z extension
mx9 - with maximum compression
%%~nxI.7z - to store the files using the sub-sub-folders name
*.txt *.html *.vtt *.srt *.zip *.jpeg *.png - file extensions to archive
after that the del deletes the archived files.

Related

How to execute all SQL files in a folder using a BATCH file and also log the result of each file in the <SQL filename>.log?

I want to execute all SQL files in a folder and its sub folder using a BATCH file and also log the result of each file in the .log.
For example, I have 2 folders Config and Data, and each has 20 SQL files (appuser.sql, tenant.sql and so on) along with a LOG empty sub-folder in each folder.
Now, I want the BATCH to execute all the SQL files and the output of each file should be logged in a separate file (appuser.log, tenant.log and so on) in the LOG sub-folder in each of the folders.
Also, suggest me the best site to learn SQL related BATCH scripting.
Thanks for your help in advance.
for /r "your_path" %%i in (*.sql) do (
sqlcmd -i "%%i" -o "log\%%i.log"
)

How to launch a .bat file in VB without associating the file with the program that launched it?

Ok, the the question looks freakish, but this is sort of a continuation of the question I posted
here.
So, I create a .bat file in Visual Studio with certain lines and launch it, but it basically doesn't find the files it needs, but if I launch the .bat file it created manually, it works.
The problem, as far as I see it, is that the .bat file the program launches isn't the same as the one that is created in the folder?
The .bat files use the command line interface of Asesprite found here, e.g. :
#set ASEPRITE="C:\Program Files\Aseprite\aseprite.exe"
%ASEPRITE% --batch animation.ase --scale 2 --save-as animation-x2.gif
I'm not sure which part of the VB code I'd need to share, so ask if needed.
The error in .bat goes something like:
C:\Users\User\Desktop\aseConverter\aseConverter\bin\Debug>"E\Asesprite\asesprite.exe" --batch skeleton2_gib3.ase --scale 1 --save-as skeleton2_gib3.gif
File not found: "skeleton2_gib3.ase"
Error loading file "skeleton2_gib3.ase"
A document is needed before --save-as arguement
The first line should not be the Debug folder, but the location the .bat file was created in. I've no idea how to fix it.
It SHOULD be
C:\Users\User\Desktop\skeleton>"E\Asesprite\asesprite.exe" --batch skeleton2_gib3.ase --scale 1 --save-as skeleton2_gib3.gif
The problem here is that the batch file references files without path. Therefore the files must be in current working directory of the batch file.
But the batch file respectively command line interpreter cmd.exe is called by the VB.net without setting the working directory. Therefore the current working directory set by Windows for the batch file is the same as of the starting VB.net application.
But the starting application creates the batch file and the other files in a different directory, not in its own current working directory.
One solution is changing current working directory inside batch file to the directory the batch file is stored. This can be done by referencing argument 0 of the batch file which contains name of batch file with complete path.
What does %~dp0 mean, and how does it work? explains how to get drive and path of batch file.
Therefore one solution is to use a batch file like below:
#echo off
cd /D "%~dp0"
set "ASEPRITE=%ProgramFiles%\Aseprite\aseprite.exe"
"%ASEPRITE%" --batch animation.ase --scale 2 --save-as animation-x2.gif
See help output after executing cd /? in a command prompt window for meaning of parameter /D (change also drive if necessary).
An explanation for %~dp0 can be read on running call /? or for /? in a command prompt window.
Another possibility would be the usage of following batch code:
#echo off
pushd "%~dp0"
set "ASEPRITE=%ProgramFiles%\Aseprite\aseprite.exe"
"%ASEPRITE%" --batch animation.ase --scale 2 --save-as animation-x2.gif
popd
The difference to command cd is explained in help which is output in a command prompt window after executing pushd /?.
Best would be to create the batch file with all files referencing with complete path, name and file extension.

How to loop through several files using gdal and cmd?

I have several files which have the tif format.I would like to translate them to ENVI format.
I succeeded for one file but I want to do for the rest of files in the directory.
the first file in the directory is:Ser_W55_20100101_A.tif.
the second file in the directory is:Ser_W55_20100102_A.tif and so on .....
I am working on windows so I just launch cmd and then wrote the command:
C:\Users>gdal_translate -of "ENVI" D:\Ser_W55_20100101_A.tif D:\Ser_W55_20100101_A.img
this worked perfectly. Any ideas please on how to do this for all files and return the same names(with changing from (tif) to ENVI)
You can make a batch file for this, do something like:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
SET mypath=D:\test\
FOR /F %%i IN ('DIR /B %mypath%*.tif') DO (
SET infile=%%i
SET outfile=!infile:.tif=.img!
gdal_translate -of "ENVI" %mypath%!infile! %mypath%!outfile!
)
This only runs in de current directory, if you want to include subfolders, add the /S flag to the dir statement. I might be good to check whats happening at first, to do so you can add a ECHO in front of the gdal cmd, so ECHO gdal_translate..., and add a PAUSE at the end. That way it will only print the commands to the console instead of actually running them.

xcopy /d copy all files every time, even unchanged files

i try this command line
xcopy e:\myfolder /EXCLUDE:excludeList.txt \\192.168.158.15\public\comp\myfolder /E/I/D/R/H/Y
the command copy all files every time even unchanged files
i use /d that suppose to copy just newer files.
a small test shows it does work. you must have other problem ...
edit
in netwoek enviroment - you should add /z

Delete temporary files on startup

I have a program which when it runs it fills Temp folder with lots of .tmp files. This is causing C drive to fill up. I have been asked to investigate if it's possible to write a script in dos to delete temporary files on startup. I also wish to delay the program starting until all files are deleted. This would need to happen every time on start-up. It would be great if this could be installed via a flash drive.
I would be grateful on any pointers on how this could be done
The little batch I am using to delete my temporary files:
#echo off
rd %temp% /s /q
md %temp%
cls
echo Temporary Files have been deleted!
echo.
pause
%temp% is a path which always results in your current temporary folder. However note that there are more temporary file locations like C:\Windows\temp.
If you just want to delete TMP files, go with del C:\<MyPath>\*.tmp.
There are probably more sophisticated ways, but the good old fashioned del c:\Temp\*.* should be a good start.
There's a list of all the options, here: http://www.computerhope.com/delhlp.htm
You will probably want /F (delete read only), /S (sub-directories) and /Q (quiet)
I assume, the following row in c:\autoexec.bat file may help:
del c:\path\to\temp\files\*.tmp
Cheers for replies. This is what I'm using
c:
cd \
cd "c:\Documents and Settings\user\Local Settings\Temp\"
del *.tmp /f/s/q
echo All tmp files deleted.
pause
This seems to do what I want it to do. Now I need it to do this everytime PC starts up. Is there a way to install this via flash drive? ie write a batch file with all commands, put on flash drive. double click .bat file, now installed and will run on startup? (Have a number of PCs which need same thing)