Newbie here needs help to manipulate file names

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

Newbie here needs help to manipulate file names

#1 Post by Lelouch » 04 Jan 2010 01:42

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#2 Post by !k » 04 Jan 2010 06:10

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

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

thanks..

#3 Post by Lelouch » 04 Jan 2010 17:15

thanks for the help..

i'll try this one now..

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

thanks..

#4 Post by Lelouch » 04 Jan 2010 17:42

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..

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#5 Post by !k » 05 Jan 2010 06:11

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

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

re

#6 Post by Lelouch » 05 Jan 2010 16:49

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

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#7 Post by !k » 05 Jan 2010 17:13

YYYYMMDDHH

And, need to find YYYYMMDDHH or YYYYMMDD ?

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

#8 Post by Lelouch » 05 Jan 2010 18:37

it needs to find YYYYMMDDHH in the filename then it will copy it on the created YYYYMMDD folder.

did you get it?

!k
Expert
Posts: 378
Joined: 17 Oct 2009 08:30
Location: Russia

#9 Post by !k » 07 Jan 2010 03:42

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

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

#10 Post by Lelouch » 11 Jan 2010 17:23

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

Lelouch
Posts: 7
Joined: 04 Jan 2010 01:28
Location: Philippines
Contact:

#11 Post by Lelouch » 11 Jan 2010 17:24

also..

sorry for the late response..

Post Reply