Konraden
Konraden

Reputation: 35

copy files and folders using only a file path (Batch)

I don't know how to turn the pathname I have (C:\Folder1\FolderNew3\3.200.25.sql) into the source directory I want to copy (C:\Folder1\FolderNew3)

I wrote this up quickly. I've never actually used a FOR or much IF statements in batch, so if these are wrong, please let me know why!

SET BUILD = 3.200.25

FOR /F %%G IN (list.txt) DO CALL :SUBROUTINE %%G
GOTO :eof

:SUBROUTINE
IF %1 == *%BUILD%.sql ( 
SET BuildLocation=%1
GOTO :CopyFiles
) ELSE GOTO :EOF

:CopyFiles
REM take %BuildLocation% and copy all files in same directory.
REM %BuildLocation% should equal "C:\Folder1\FolderNew3\3.200.25.sql" I want to copy C:\Folder1\FolderNew3\ 
XCOPY /S /Y [source] C:\MyNewLocation\


REM    Example contents of list.txt
REM    C:\Builds\Folder2\2.200.25.sql
REM    C:\Builds\Folder1\1.200.25.sql
REM    C:\Builds\Folder3\SubFolder\3.200.25.sql

I'm dumping a directory listing to list.txt and am looking for a set of files and folders located alongside a uniquely identifiable file (in this case, %build%.sql)

The build is a unique identifier so it will only match once.

I need to copy all the folders located alongside the sql script that is stored in the parent directory.

The parent directory location may be of variable length, so I can't use a delim=\ to parse the path name that I know of.

Upvotes: 3

Views: 374

Answers (1)

malexander
malexander

Reputation: 4680

To Set BuildLocation to the parent dir try changing this line:

SET BuildLocation=%1

To:

SET BuildLocation=%~p1

From this site:

%~pI - expands %I to a path only

Upvotes: 1

Related Questions