A bat that copies a file to a folder named after it

Asked

Viewed 185 times

-2

Hello, I’m trying to create a bat that copies a file from a folder and creates a folder with the name of that file, and plays that file inside it but it’s not working very well and I need help

    @echo on
:: Script de cópia do arquivo principal para a pasta no destino, com o mesmo nome do arquivo.

set dir_orig=C:\teste\
set dir_arq=C:\teste\%1

arquivo="BCB _20200115.zip"

:: Verifica se o diretorio não existe
IF NOT EXIST %dir_arq%\ md %dir_arq%

robocopy %dir_orig%\ %dir_arq% %arquivo%

pause
exit /B

2 answers

0

See if that’s what you want: Video

Here is the code:

@echo on
:: Script de cópia do arquivo principal para a pasta no destino, com o mesmo nome do arquivo.


set dir_orig=C:\teste\
set dir_arq=C:\teste\BCB _20200115\
arquivo=BCB _20200115.zip

:: Verifica se o diretorio não existe
IF NOT EXIST "%dir_arq%\" md "%dir_arq%"

robocopy "%dir_orig%\" "%dir_arq%\%arquivo%"

pause
exit /B

0

@echo off 

cd /d "%~dp1"

set "dir_orig=C:\teste\" & set "dir_arq=C:\teste\%~n1"

:: arquivo="BCB _20200115.zip"

:: Verifica se o diretorio não existe
IF NOT EXIST %dir_arq%\ md %dir_arq%
2>nul mkdir "%dir_arq%"

robocopy "%dir_orig%" "%dir_arq%" "%~nx1"

pause & goto=:EOF

1. Enter the drive/folder/ where your file is using the expansion of your argument: cd /d "%~dp1"

2. You already have the nfile name in your argument for expansion %~n: %~n1

3. Do not need to test if the directory exists, create already omitting possible error: 2>nul mkdir ...

4. don’t use exit /... use goto=:EOF (Go To End Of File)

Browser other questions tagged

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