Page 1 of 1
Newbie here needs help to manipulate file names
Posted: 04 Jan 2010 01:42
by Lelouch
good day to everyone..
i'm know someone could help me out there about my problem.
i'm a newbie regarding dos batch scripting so i don't really know how to solve my prob..
what i need to do is to search a file in a folder and and copy it to another folder..not all files must be copied..i need to specifically pick the file, i.e.;
INSITE-PH01_1_20090715100000_001
INSITE-PH01_1_20090715100000_002
INSITE-PH01_1_20090715100000_003
INSITE-PH01_1_20090716100000_001
INSITE-PH01_1_20090716100000_002
in this case i need to copy the files with recent date, and that would be:
INSITE-PH01_1_20090716100000_001
INSITE-PH01_1_20090716100000_002
is this possible?
Any help that anyone can offer, is truly appreciated.
Thank you!
Lelouch
Posted: 04 Jan 2010 06:10
by !k
dirty code uses hard break:
Code: Select all
@echo off
setlocal enableextensions
set "indir=c:\in dir"
set "outdir=d:\out dir"
set "begin=INSITE-PH01_1_20"
chdir /d "%indir%"
for /f "delims=" %%f in ('dir /b/o-n %begin%????????????_???.*') do call :f "%%f"
:f
set "n=%~n1"
set "l=%n:~0,-10%"
copy /b "%l%??????_???.*" "%outdir%" &EXIT
thanks..
Posted: 04 Jan 2010 17:15
by Lelouch
thanks for the help..
i'll try this one now..
thanks..
Posted: 04 Jan 2010 17:42
by Lelouch
someone gave me a code and i edited it..
and I finally solved my problem somehow..
Code: Select all
@echo off
set SourcePath=d:\deck\TEST\Source
set Destination=d:\deck\TEST\20090918
set Insite=2009091814
IF NOT EXIST %Destination% GOTO NOPATH
:GETUPDATE
echo "updating local files..."
COPY /Y %SourcePath%\*%Insite%*.txt %Destination%
:NOPATH
echo "creating %Destination%..."
mkdir %Destination%
echo "copying files..."
COPY /Y %SourcePath%\*%Insite%*.txt %Destination%
I'll only need to change the value of the insite depending on the date that i need to copy..
Posted: 05 Jan 2010 06:11
by !k
I ceased to understand what you need.
Copy files from the last date in the name or a specific date?
And yet, that means "14" in the Insite=2009091814
re
Posted: 05 Jan 2010 16:49
by Lelouch
Copy files from the last date in the name or a specific date?
specific date...
And yet, that means "14" in the Insite=2009091814
the format of:
2009091814
is
YYYYMMDDHH
Posted: 05 Jan 2010 17:13
by !k
YYYYMMDDHH
And, need to find YYYYMMDDHH or YYYYMMDD ?
Posted: 05 Jan 2010 18:37
by Lelouch
it needs to find YYYYMMDDHH in the filename then it will copy it on the created YYYYMMDD folder.
did you get it?
Posted: 07 Jan 2010 03:42
by !k
Code: Select all
@echo off
setlocal enableextensions
set "SourcePath=d:\deck\TEST\Source"
set "DestinPath=d:\deck\TEST"
if "%~1"=="" (echo Need input YYYYMMDDHH, ex. %~nx0 2009091814 &exit /b)
set "Insite=%~1"
set "Day=%Insite:~0,8%"
if not "%Day%"=="%Insite:~0,-2" (echo Wrong input. Need 10 simbols, ex. 2009091814 &exit /b)
set "Destination=%DestinPath%\%Day%"
if exist %Destination% goto :getupdate
:nopath
echo "Creating %Destination%..."
mkdir "%Destination%"
:getupdate
echo "Copying files..."
copy /y "%SourcePath%\*%Insite%*.txt" "%Destination%" >nul
Untested
Posted: 11 Jan 2010 17:23
by Lelouch
thanks for the code..
hehe..i didn't understand some of the code..
sorry i'm new in batch scripting..
by the way, i manage to edit my previous code to achieve what i want..
here's the code of what i've done:
Code: Select all
@echo off
set Hour=%time:~0,2%
if %Hour% lss 10 (set Hour=0%time:~1,1%)
set Min=%time:~3,2%
set Sec=%time:~6,2%
set FinHr=%Hour%
set Dte=%date%
set FinDte=%Dte:~10,4%%Dte:~4,2%%Dte:~7,2%
set SourcePath=d:\deck\TEST\Source
set Destination=d:\deck\TEST\%FinDte%
set Insite=%FinDte%%FinHr%
IF NOT EXIST %Destination% GOTO NOPATH
:GETUPDATE
echo "updating local files..."
COPY /Y %SourcePath%\*%Insite%*.txt %Destination%
goto END
:NOPATH
echo "creating %Destination%..."
mkdir %Destination%
echo "copying files..."
COPY /Y %SourcePath%\*%Insite%*.txt %Destination%
goto END
:END
this is the final code of my small project..
!k
-thanks for the codes and responses..it's been a great help for me..
sorry for my english
Posted: 11 Jan 2010 17:24
by Lelouch
also..
sorry for the late response..