How to loop through several files using gdal and cmd? - gdal

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.

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.

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)

How do I copy the newest file in a directory somewhere else from the command line in windows vista

I have a directory of many files in Windows Vista. I'd like in a batch script to be able to pick the newest file and copy it to another location. Any ideas how I do that?
You can use the for command to invoke a directory listing that is sorted by date, and use it to set an environment variable, if you set the same variable to each file, then it will end up being set to the latest file.
put this into a batch file:
for /F "delims=" %%I in ('dir /b /a-d /od') do set LATEST=%%I
echo "%LATEST%"
then you can use the move command to move that file to wherever you want it.
move "%LATEST%" wherever
EDIT 10-Feb-2011: fixed to handle filenames with spaces in them. The fix is to use "delims=" to disable tokeninzing on space. Thanks to Dave Potts for the fix.