B
B
byulent2016-02-29 19:06:31
cmd/bat
byulent, 2016-02-29 19:06:31

How to get information about a file from a subfolder using bat?

Need to get last time. file changes from the specified subfolder and compare it with the time of the file of the same name in the current folder. If the file in the current folder is newer, the file in the subfolder is replaced by it. All this is done in a loop for: Everything is clear with the current folder: . But what to do with the nested folder, I just don’t understand. Googled about the existence of the modifier , as well as about the delayed expansion of variables using . Everything seems to be fine, but the combination (where is the path to the desired folder) gives me the date along with the path to the file. And an attempt to cut a string in a view results in... ! What to do with it? Full code:
for %%x in (*.txt) do
%%~tx
%~$Path:I!var!set time2 = %%~t$dir:x$dir!time2:~0,16!
time2=~0,16

rem @echo off
setlocal enabledelayedexpansion
for %%x in (*.txt) do (
if not exist %2 md %2
set dir1=%cd%\%2
    if not %%x==%1 (
        if exist %2\%%x (
            set time1 = %%~tx
            set time2 = %%~t$dir1:x
            set time2 = !time2:~0,16!
            set time2
            if time1 GTR time2 copy %%x %2
        ) else copy %%x %2
    )
    rem pause
    if errorlevel 1 echo Ошибка копирования файла
)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2016-02-29
@res2001

Let's say you have the full path to the file in %%x, then
%%~dpx - drives and directory, without a file name
%%~nxx - file name and extension.
Thus, to get the full name of the same file in a subdirectory, you need to build the following construction:
%%~dpxSubfolder\%%~nxx
where Subfolder is the name of the subdirectory.
To get the date/time from this construction, put it in a for:
for %%y in ("%%~dpxSubfolder\%%~nxx") do echo %%~
ty not fundamental.
For modifiers, see for /?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question