Batch file anchored

Asked

Viewed 183 times

1

How do I get a batch anchored to a folder for a backup operation on a USB stick where the drive letter always changes example:

@echo off
mkdir ..\subs
move /y .\*.srt ..\subs
pause
exit
  • What do you mean anchored?

  • I don’t know why but when vc simply runs the bat cmd runs from the user folder c:\users\joao and not the folder in which the bat.

2 answers

0


You can use the command pushd to change the working directory, pass %~dp0 to indicate the file directory .bat, see an example:

@echo off

echo Diretorio atual %cd%
echo Diretorio do script %~dp0

REM Altera o diretório atual de trabalho
pushd %~dp0

echo Diretorio atual %cd%
pause

When using pushd %~dp0 you indicate that the current directory is the location where the file is located.

Exit:

Diretorio atual C:\Users\<usuario>
Diretorio do script C:\Users\<usuario>\local\do\batch\
Diretorio atual C:\Users\<usuario>\local\do\batch\

Pressione qualquer tecla para continuar. . .

0

The flash drive has to have some brand to be checked. It can be like this: You create the subs folder on the flash drive that you will use for backup and place the code below in the folder.

@echo off
if EXIST d:\subs\*.srt set PENDRIVE=D:&goto backup
if EXIST e:\subs\*.srt set PENDRIVE=E:&goto backup
if EXIST f:\subs\*.srt set PENDRIVE=F:&goto backup
if EXIST g:\subs\*.srt set PENDRIVE=G:&goto backup
rem Acrescente outros Drives se quiser
echo NENHUM PENDRIVE ENCONTRADO.
goto fim
:backup
move /y .\*.srt %PENDRIVE%\subs
:fim
pause

Browser other questions tagged

You are not signed in. Login or sign up in order to post.