assign variable to multiple line in input fil

Unitech Pt630d mobile computer, batch, 1...
June 15, 2010 at 00:19:21
Specs: Windows XP
hi,
i juz learnt about batch files..I need your help to assist..
My problem is, i need to assign variable from the input files which contain multiple lines..each line i need to assign as variable and output the variable that need to be concatenated...

e.g:
(input file)
hello yong
my name is
nanny they
i need your help

so, i need to assign" hello yong " as 1st variable, "my name is" as second variable and so on..

the output should be..
hello yong, i need your help ( which need to concatenate 1st variable with the forth variable)..

really need your help..
tanx :)


See More: assign variable to multiple line in input fil

Report •


#1
June 15, 2010 at 10:26:23
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET filename=%~dpn0.txt   &REM Set this to the filename you want concatenated
SET output=
FOR /F "usebackq tokens=* delims=" %%a IN ("%filename%") DO (
    SET output=!output!%%a 
)
ECHO !output!
EXIT /B

Note: There is a space after %%a in the statement

SET output=!output!%%a 

Whoops! Missed that you needed 4th variable...


Report •

#2
June 15, 2010 at 10:35:27
Second attempt after reading requirements:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET filename=%~dpn0.txt   &REM Set this to the filename you want concatenated
SET output=
SET /A counter=0
FOR /F "usebackq tokens=* delims=" %%a IN ("%filename%") DO (
    SET /A counter+=1
    IF "!counter!" EQU "1" (
        SET output=!output!%%a 
    ) ELSE (
        IF "!counter!" EQU "4" (
            SET output=!output!%%a 
        )
    )
)
ECHO !output!
EXIT /B


Report •

#3
June 15, 2010 at 23:37:15
Sorry...I've tried to run the code given but there's error..
I've found that the code below display what is exactly i meant..but i don't really understand what is actually variable to assign..

my input file will be like this: [input.txt]

1000 top
Properties
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 253

# the example above is only for 1 input..there's actually thousands,so need to loop...

and the desired output for each input is:

file name : More Power Began with V \ 6/23/08 3:05:30 PM
location: c:\ Output\e_46\183\250\294 \~ry35453245849584.pdf

where: file name = SI_NAME \ + SI_CREATION
and location = c:\ + SI_PATH \ + SI_FILES

below are the script that i've tried to use..but i'm not really sure on how to assign the variable as the result will be error...it will not give me the desired output..

SETLOCAL EnableDelayedExpansion
FOR /F %%F IN ("C:\input.txt") DO (
SET G=%%F
IF "!G:~-10,-3!" == "SI_FILES" (
set name=%G
)
IF "!G:~-10,-3!" == "SI_PATH" (
set path=%G
concatenate path + name
copy to destination

)

)




Report •

Related Solutions

#4
June 16, 2010 at 06:33:06
Try this:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /R "C:\Root\Folder\of\Target\Files" %%a IN (*.txt) DO (
    FOR /F "usebackq tokens=1*" %%b IN ("%%a") DO (
        IF "%%b" EQU "SI_NAME"      (SET file_name=%%c)
        IF "%%b" EQU "SI_PATH"      (SET file_path=%%c)
        IF "%%b" EQU "SI_FILES"     (SET file_files=%%c)
        IF "%%b" EQU "SI_CREATION"  (SET file_date=%%c)
    )
    ECHO.File Name: %file_name% \ %file_date%
    ECHO.Location : %file_path% \ %file_files%
)
EXIT /B

This should check all folders starting at "C:\Root\Folder\of\Target\Files", looking for any TXT files. When a TXT file is found, it looks for the specific strings at the beginning of the line, and sets the value of the variable to the rest of the line.


Report •

#5
June 16, 2010 at 07:22:47
i've tried to run the script but it seems like the output doesn't appear..

The output result is;

File Name: \
Location: \

instead of all the variable attached at the assigned line.

Besides that, can you explain to me what is the difference between the %%a,%%b,%%c??


Report •

#6
June 16, 2010 at 09:42:53
My bad! Change:

    ECHO.File Name: %file_name% \ %file_date%
    ECHO.Location : %file_path% \ %file_files%

to
    ECHO.File Name: !file_name! \ !file_date!
    ECHO.Location : !file_path! \ !file_files!

Concerning the different variables:
%%a is used in the first FOR /R loop, and will represent the
filename values matching what's inside the ( ), in this case
*.txt. So in other words, for every .txt file you find in
"C:\Root\Folder\of\Target\Files" (and subfolders), do
something with it, and by the way, the filename will be
represented by variable %%a.

This sets up the next FOR /F loop. This reads line by line the
contents of the file "%%a", and splits the line into 2 variables:
%%b and %%c. This is done by using the "tokens=1*", which
means take the first word in the line (since I didn't specify any
delimiters), and assign the variable %%b to represent it's
value, and assign the rest of the line to variable %%c. So
looking at "tokens=1*":
1 = %%b = first word,
* = %%c = rest of line.

Now %%b can be tested against constants, such as "SI_NAME", and actions taken, such as assigning new variables that can be concatenated (and ECHOed) later.

HELP FOR is a little overwhelming at first glance, but once
you start playing with FOR, it comes in pretty handy!


Report •

#7
June 16, 2010 at 18:13:27
tanks orangeboy..really appreciate it!
Hope you can guide me until i successfully build the system..

There is question I wanted to ask you..
what if the line contain space, in your example, IF "%%b" EQU "SI_PATH" (SET file_path=%%c); it will output the string after the "SI_PATH" which is frs://Output/e_46/183/250/294/ ..but i just want the output to start with Output/e_46/183/250/294/ without the frs:// at the beginning... i've tried to use IF "%%b" EQU "SI_PATH frs://" (SET file_path=%%c) but it not output anything..

I also tried to use IF "%%b" "!b:~13,27!" EQU "SI_PATH" (SET file_path=%%c) and there is still error occur..


Report •

#8
June 16, 2010 at 20:49:19
Hmm... If the SI_PATH is always frs://, or is
always 6 characters long, you could manipulate the
resulting variable using a substring:

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /R "C:\Root\Folder\of\Target\Files" %%a IN (*.txt) DO (
    FOR /F "usebackq tokens=1*" %%b IN ("%%a") DO (
        IF "%%b" EQU "SI_NAME"      (SET file_name=%%c)
        IF "%%b" EQU "SI_PATH"      (SET file_path=%%c
                                     SET file_path=!file_path:~6!)
        IF "%%b" EQU "SI_FILES"     (SET file_files=%%c)
        IF "%%b" EQU "SI_CREATION"  (SET file_date=%%c)
    )
    ECHO.File Name: !file_name! \ !file_date!
    ECHO.Location : !file_path! \ !file_files!
)
EXIT /B

To explain:

SET file_path=!file_path:~6!

"file_path" is set to the value of "!file_path!", but starting at
position 6 and ending with the remainder of the string. Another
way to look at it is if I put:

SET file_path=!file_path:~6,6!

That would set file_path to
the value of "!file_path!" starting at position 6 and ending after
only 6 characters, leaving file_path as "OUTPUT". Take a look
at HELP SET for other ways of using substrings.

Now, if SI_PATH is NOT always frs://, or is NOT
always
6 characters long, it could get a bit more
complicated, involving multiple IF statements, but more than
likely an additional FOR /F loop, using !file_path! as the input
string...


Report •

#9
June 16, 2010 at 21:04:48
orangeboy, you're so great!!

Thank you very much..really appreciate it :)

now, i'm trying to replace the character in the input file from "/" to "\"..

example; c:/ Output/e_46/183/250/294 /~ry35453245849584.pdf

so that the output for location like this;
c:\ Output\e_46\183\250\294 \~ry35453245849584.pdf
...

i've tried to manipulate this script in the coding..

SET input.txt
Set v_replacement=\
SET v_result=%input.txt:/=!v_replacement!%
SET v_result=%%a..

is it right?



Report •

#10
June 16, 2010 at 21:58:31
It'd be easiest in the same script by adding one more line:
        IF "%%b" EQU "SI_PATH"      (SET file_path=%%c
                                     SET file_path=!file_path:~6!
                                     SET file_path=!file_path:/=\!)

Note there is no tilde (~) in the string substitution line:

"SET file_path=!file_path:/=\!"


Report •

#11
June 16, 2010 at 23:13:46
how if one of the input file contains more than 1 information..Below are the example in the input file:


assume (input.txt)

1 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 253


2 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 254


3 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 255


4 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 256

5 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 257


6 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 258


7 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 259


8 top
Properties1
SI_CREATION 6/23/08 3:05:30 PM
SI_NAME More Power Began with V
SI_FILES ~ry35453245849584.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 260


..
...
...
...

10000 top
Properties1
SI_CREATION 6/23/10 3:05:30 PM
SI_NAME More Power Began with z
SI_FILES ~ry35453245899009.pdf
SI_PATH frs://Output/e_46/183/250/294/
SI_VALUE 67272
SI_NUMFILES 1

SI_PARENTID 500

where should i assign the for loop in order for the script to read all the information in the file?
should i assign before :
FOR /F "usebackq tokens=1*" %%b IN ("%%a") DO

or after it??

i did try an error in order to obtain the actual script..but there's still in valid command.. Correct me if i'm wrong..

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION



FOR /F ["eol=; tokens=0,1* delims=,"] %%a IN ("c:\Root\Folder\of\Target\Files\input.txt") DO (

FOR /L %%a IN (1,1,10) DO (

IF "%%b" EQU "SI_NAME" (
SET file_name=%%c
)

IF "%%b" EQU "SI_PATH" (
SET file_path=%%c
SET file_path=!file_path:~6,27!
SET file_path=!file_path:/=\!
)

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
)

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c
SET file_date=!file_date:/=-!
)

)


ECHO.File Name: !file_name!\!file_date!
ECHO.Location : !file_path!!file_files!

)

EXIT /B


thanks.


Report •

#12
June 17, 2010 at 03:04:12
it's ok... i successfully edit the script and managed to output all the information in the input files.. :)
tanx!

Report •

#13
June 17, 2010 at 06:01:33
hi orangeboy!

i need your guidance again..
This is the most crucial part where i have to copy the source of the location to the destination folder..

by the way,below are the script that i have edited..

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "usebackq tokens=1*" %%b IN ("c:\Root\Folder\of\Target\Files\input.txt") DO (

IF "%%b" EQU "SI_NAME" (
SET file_name=%%c
)

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
)

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c
SET file_date=!file_date:/=-!
)


IF "%%b" EQU "SI_PATH" (
SET file_path=%%c
SET file_path=!file_path:~6,27!
SET file_path=!file_path:/=\!

ECHO:
ECHO.File Name: !file_name!\!file_date!
ECHO:
ECHO.Location : F:...\...\... \!file_path!!file_files!
ECHO:
ECHO:
ECHO:
)




)

EXIT /B

and the output will be like this;
p/s: the input file contain 6 info

File Name: More Power Began with V\6-26-09 3:05:30 PM

Location : C:\Output\e_46\183\250\294\~ry354532456530c31c.pdf


File Name: More Power Began with V\6-26-09 3:06:42 PM

Location : C:\Output\a_11\113\090\290\~ry354532456LOI891c.pdf


File Name: More Power Began with V\6-27-09 12:20:17 AM

Location : C:\Output\a_12\119\009\306\~ry35453245653I8U7Y.pdf


File Name: More Power Began with V\6-28-09 12:20:23 AM

Location : C:\Output\a_00\109\770\300\~ry35453245653O9IOU.pdf


File Name: More Power Began with V\6-29-09 12:20:27 AM

Location : C:\Output\a_18\116\033\378\~ry35453245653EFR46.pdf


File Name: More Power Began with V\6-30-09 12:20:33 AM

Location :C:\Output\a_00\191\145\496\~ry3545324565312345.pdf

so, based on the location which is also known as the [source]
example:

Location :C:\Output\a_00\191\145\496\~ry3545324565312345.pdf

i need to copy the file/report from the source to the destination folder..

can u guide me on this?


i've tried to create parameter for destination folder but i got stuck half way..

correct me if i'm wrong..

really need your help to explain to me on this matter...thanks alot :)



Report •

#14
June 17, 2010 at 08:53:29
Just so I fully understand, you want to copy the existing files
"!file_files!" found at "!file_path!" to a new location, \"!file_name!.pdf"?

So given:

1000 top 
Properties 
SI_CREATION 6/23/08 3:05:30 PM 
SI_NAME More Power Began with V 
SI_FILES ~ry35453245849584.pdf 
SI_PATH frs://Output/e_46/183/250/294/ 
SI_VALUE 67272 
SI_NUMFILES 1 

SI_PARENTID 253

you want:

\\Output\e_46\183\250\294\~ry35453245849584.pdf

to be copied to:
F:\Program Files\Crystal Report Server\Business Objects Enterprise 11.5\FileStore\More Power Began with V.pdf

Insomniac at large


Report •

#15
June 17, 2010 at 17:51:10
ok...
"~ry35453245849584.pdf " is the file that resides in the location F:\...\...\...\...\...\~ry35453245849584.pdf..( location that obtain from the output "location"..)

so, i have to copy this file to another new location which i have to create new destination folder to store all the files...

can you guide me on this?


Report •

#16
June 17, 2010 at 19:14:55
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET Dest= "C:\root"

FOR /F "usebackq tokens=1*" %%b IN ("c:\Root\Folder\of\Target\Files\input.txt") DO (

IF "%%b" EQU "SI_NAME" (
SET file_name=%%c
)

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
)

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c
SET file_date=!file_date:/=-!
)


IF "%%b" EQU "SI_PATH" (
SET file_path=%%c
SET file_path=!file_path:~6,27!
SET file_path=!file_path:/=\!

ECHO:
ECHO.File Name: !file_name!\!file_date!
ECHO:
ECHO.Location : C:\!file_path!!file_files!
ECHO:
ECHO:
ECHO:


COPY %Location% %Dest%


)

)

EXIT /B

i've tried this but it doesn't work..


Report •

#17
June 17, 2010 at 19:56:13
Like this?

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

SET Dest=C:\Root\Folder\of\Output\Files
FOR /F "usebackq tokens=1*" %%b IN ("c:\Root\Folder\of\Target\Files\input.txt") DO (
    FOR /F "usebackq tokens=1*" %%b IN ("%%a") DO (
        IF "%%b" EQU "SI_NAME"      (SET file_name=%%c)
        IF "%%b" EQU "SI_PATH"      (SET file_path=%%c
                                     SET file_path=!file_path:~6!
                                     SET file_path=!file_path:/=\!)
        IF "%%b" EQU "SI_FILES"     (SET file_files=%%c)
        IF "%%b" EQU "SI_CREATION"  (SET file_date=%%c
                                     SET file_date=!file_date:/=-!
                                     SET file_date=!file_date::=.!)
    )
    ECHO.File Name: !file_name! \ !file_date!
    ECHO.Location : !file_path! \ !file_files!
    ECHO.
    IF NOT EXIST "%Dest%\!file_name!-!file_date!" MKDIR "%Dest%\!file_name!-!file_date!"
    ECHO.Copying "!file_path!!file_files!" to "%Dest%\!file_name!-!file_date!"
    COPY "!file_path!!file_files!" "%Dest%\!file_name!-!file_date!"
)
EXIT /B

Insomniac at large


Report •

#18
June 17, 2010 at 21:52:27
orangeboy,
i've tried to run your script but it keeps looping many times..Thus I've edited the script a bit so that i will output exactly the quantity of the input..

below are my script:

@ECHO OFF

SETLOCAL ENABLEDELAYEDEXPANSION

SET Dest=C:\root\Files\....\...etc

FOR /F "usebackq tokens=1*" %%b IN ("C:\root\folder\of\target\files\input.txt") DO (

IF "%%b" EQU "SI_NAME" (
SET file_name=%%c
)

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
SET file_ext=!file_files:~28!
)

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c
SET file_date=!file_date:/=-!
SET file_date=!file_date::=.!
SET file_dat=!file_date:%yyyy%%mm%%dd%)


IF "%%b" EQU "SI_PATH" (
SET file_path=%%c
SET file_path=!file_path:~6,27!
SET file_path=!file_path:/=\!

ECHO:
ECHO.Name of File: !file_name!\!file_date!
ECHO:
ECHO.Location : C:\!file_path!!file_files!
ECHO:
COPY "C:\!file_path!!file_files!" "%Dest%\!file_name!!file_date!!file_ext!"
ECHO:
ECHO:


))

EXIT /B

BUT
based on my script, i tried to rename the file that i wanted to copy..

the format is like this..

file name+ date(only)+extension of the file..
the date format is in yyyy-mm-dd


how to overcome this problem?
guide me pls..


Report •

#19
June 17, 2010 at 23:08:42
sorry...my bad! i've found da way!
thanx! :)

Report •

#20
June 17, 2010 at 23:43:34
orangeboy,
my input for time is
SI_CREATION_TIME 3/13/08 12:20:27 AM

which is in month/day/year format...
i wanted to change the format in order for me to rename the file..

below are my code..


IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c

SET file_date=!file_date:/=-!
SET file_date=!file_date::=-!

SET file_year=!file_date:~5,2!
SET file_year=20!file_year!

SET file_day=!file_date:~2,2!
SET file_time=!file_date:~8,8!
SET file_month=!file_date:~0,2!

)

it successfully output the desired output but i realize that the month is actually in march which consist of only 1 digit..that's y i set the month as SET file_month=!file_date:~0,2! to obtain the month only...i'm considering about the month for October/November n December where it contain 2 digits..

when i change the number in !file_date:~0,2! , the output is not as desired..

do you have any idea for this issue??


Report •

#21
June 18, 2010 at 10:08:42
Always pad the month with a leading zero (...08,09,010,011,012):
SET file_month=0!file_date:~0,2!

Then trim the month to only last two positions (...08,09,10,11,12):
SET file_month=!file_month:~-2!

Put together:

SET file_month=0!file_date:~0,2!
SET file_month=!file_month:~-2!

Insomniac at large


Report •

#22
June 20, 2010 at 18:16:25
orangeboy,

this is my script:

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c


SET file_date=!file_date:/=-!
SET file_date=!file_date::=-!

SET file_year=20!file_date:~5,2!

SET file_month=0!file_date:~0,2!
SET file_month=!file_month:~-2!

SET file_day=0!file_date:~2,2!
SET file_day=!file_day:~-2!

SET file_time=!file_date:~8,8!

)

// script to output:
COPY "C:\!file_path!!file_files!" "%Dest%!file_year!-!file_month!-!file_day!-!file_time!"

the input is:

SI_CREATION_TIME 6-26-08 3-06-42 PM

i run the script and it return the output well..
the output is

2008-6-26-3-06-42

but if the input is in october/november/december..
it will output wrongly..(let say the data is in november instead of june)

the output will be : 20-0-11-2- 3-06-42

i did try the example that u gave me, below are my script that i've edited..


IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c


SET file_date=!file_date:/=-!
SET file_date=!file_date::=-!

SET file_year=20!file_date:~5,2!
SET file_year=!file_year:~-4!

SET file_month=0!file_date:~0,2!
SET file_month=!file_month:~-2!

SET file_day=0!file_date:~2,2!
SET file_day=!file_day:~-2!

SET file_time=!file_date:~8,8!

)

but the output seems to be wrong..

the output that i obtained is:

20-0-11 --2- 3-06-42
2008-6- -27-12-20-17 ( input:SI_CREATION_TIME 6-27-08 12-20-17 AM)

really need your guidance and explanation as soon as possible..
thanks!


Report •

#23
June 20, 2010 at 20:27:04
this is the other script that i've tried..
but the output for month, it doesn't have "0" in front of the digit..

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c

SET file_date=!file_date:/=-!
SET file_date=!file_date::=-!

SET file_year=20!file_date:~5,2!
SET file_year=!file_year:~-4!

SET file_month=0 !file_date:~0,2!
SET file_month=!file_month:~-2!

SET file_day=0!file_date:~2,2!
SET file_day=!file_day:~-2!

SET file_time=!file_date:~8,8!
)

output = 2008-6-26-3-05-30
supposed to be 2008-06-26-3-05-30

but still i couldn't manage to make the output correct if the month is 10,11 and 12..


Report •

#24
June 20, 2010 at 20:34:10
i think the script below should work if the "0" is assigned in front of the month..
Can you tell me how to make the zero appear..i did try at the year par, by using 20 and it works! but when i assigned "0", it doesn't appear the number zero..

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
SET file_ext=!file_files:~19,4!
)

IF "%%b" EQU "SI_CREATION_TIME" (
SET file_date=%%c

SET file_date=!file_date:/=-!
SET file_date=!file_date::=-!

SET file_year=20!file_date:~6,2!
SET file_year=!file_year:~-4!

SET file_month=0!file_date:~0,2!
SET file_month=!file_month:~-2!

SET file_day=0!file_date:~2,2!
SET file_day=!file_day:~-2!

SET file_time=!file_date:~8,8!
)


Report •

#25
June 20, 2010 at 21:10:23
orangeboy, based on your explanation on response 21, i've found what is the root of the problem..

here are the inputs:

SI_CREATION_TIME 6-26-08 3-06-42 PM (in june)
SI_CREATION_TIME 11-26-08 3-06-42 PM (in november)

let say i pad the month with 0, and trim the month the last two digits, like u mentioned,

SET file_month=0!file_date:~0,2!
SET file_month=!file_month:~-2!

i realize that, when i pad 0 to every month, for month june, and it will trim the laz two digits, the digits that it trim is actually ( 6- )in 06- instead of 06..

that's why it output the wrong result..

the script works well if the month is 10,11,and 12 as it will take the last two digits exactly to be used as desired output..

i've tried to manipulate this problem but it seems like making me more confuse..

Need your help..thanks!


Report •

#26
June 20, 2010 at 22:20:58
Yes, that makes sense. Another for loop should get the date
information straightened out:

...
IF "%%b" EQU "SI_FILES" (
    SET file_files=%%c
    SET file_files=!file_files:~9!
    SET file_ext=!file_files:~19,4!
)
IF "%%b" EQU "SI_CREATION_TIME" (
    SET file_date=%%c
    SET file_date=!file_date:/=-!
    SET file_date=!file_date::=-!
    FOR /F "tokens=1-3 delims=-" %%d IN ("!file_date!") DO (
        SET file_month=0%%d
        SET file_month=!file_month:~-2!
        SET file_day=0%%e
        SET file_day=!file_day:~-2!
        SET file_year=20%%f
    )
    SET file_time=!file_date:~8,8!
)
...

The bolded code will take the variable !file_date! and break it into 3 parts represented by variables %%d, %%e, and %%f, using dash (-) as a delimiter. file_month and file_day will be padded with a leading 0, then trimmed. Since dash is being used as a delimiter, it gets removed from the resulting %%* variables. You'll need to add them back in:
SET something=!file_year!-!file_month!-!file_day!
HTH!

Insomniac at large


Report •

#27
June 20, 2010 at 23:29:44
thank you, it really helps!! :D

Report •

#28
June 21, 2010 at 17:43:47
orangeboy..let say i want to make my script more dynamic..
instead of have to count the letters in a word, is there any way to do it?

for example..

IF "%%b" EQU "SI_FILES" (
SET file_files=%%c
SET file_files=!file_files:~9!
SET file_ext=!file_files:~19,4!
)

input : SI_FILES SI_FILE1 ~rt23hy923i246a1352.pdf

based on the above script, i have to count it manually in order to find the extension..

i also did try to do like this.. SET file_ext=( "." dot) !file_files:~3! in order to get the ext.

but i'm considering if there are many other reports that have different value of characters in the report files..


Report •

#29
June 21, 2010 at 19:43:42
The CALL command allows you to parse a file into it's two
parts: it's name, and it's extension. It uses %~n and %~x to
get those parts. Here's an example:

@ECHO OFF
SETLOCAL

SET full_file_name=This.bat

CALL :Get_File_Ext "%full_file_name%"

ECHO File name=%file_name% 
ECHO File extension=%file_extension%

EXIT /B

:Get_File_Ext
SET file_name=%~n1
SET file_extension=%~x1
GOTO :EOF

When you CALL something, it can accept arguments. In this
case, I'm passing the variable %full_file_name% (actually the
value of that variable since it's surrounded by %) as an
argument to the routine ":Get_File_Ext". ":Get_File_Ext"
places that argument into the variable %1, which can than be
manipulated using %~n1 and %~x1. ":Get_File_Ext" should
take care of ANY filename and extension you throw at it,
including files like "Some.file.with.more.than.one.dot.in.it.txt",
or "Some file with a short extension.s". Type HELP CALL to
see other %~ options you have. They're listed at the end of
the help...

Insomniac at large


Report •

#30
June 21, 2010 at 22:35:19
orangeboy..
i use the same script in order to call the time:


SET file_time=!file_date:~8,8!

FOR /F "tokens=1-3 delims=-" %%g IN ("!file_time!") DO (

SET file_hour=0%%g
SET file_hour=!file_hour:~-2!

SET file_min=0%%h
SET file_min=!file_min:~-2!

SET file_sec=0%%i
SET file_sec=!file_sec:~-2!


)


the script sometimes work well in the hour n min but not in sec...where the number "0" doesn't appear..

example of the input is:

1) SI_CREATION_TIME 1/9/10 6:00:43 AM
and the output will be like this : 2010-01-09-01- A-0

instead of 2010-01-09-06-00-43 (the correct output)

but the output is correct when the input is :
SI_CREATION_TIME 6/27/08 3:20:23 AM

which is 2008-06-27-03-20-23

I also wondering that when i run the script using thousands of input files, some of the output is not correct..

example:

Input: SI_CREATION_TIME 11/22/09 2:06:33 AM
Output: 2009-11-22- 2-06-03 (should be 2009-11-22-02-06-33[-have zero in the hours part])

I've tried to identify what is actually the problem but it seems like I'd stuck in the middle.

one more things, I'm facing problem in order to assign what is exactly the number for sub string in time as the date itself is not fixed.

What i mean is that sometimes the input date is 1/2/10, thus in order to read the time need to assume/set the sub string as (7,8)..

but if the input date is 12/23/09, there is another condition that i have to consider..



Report •

#31
June 22, 2010 at 08:06:11
The following will break down and display the bare variables I
think you want:

[code]

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=1*" %%a IN ('DIR /S /B "\Your\Path\To\*.txt"') DO (
    :Split constants and variables
    FOR /F "usebackq tokens=1*" %%b IN ("%%a") DO (
        IF "%%b" EQU "SI_NAME"      (SET file_title=%%c)
        IF "%%b" EQU "SI_PATH"      (SET file_path=%%c
                                     SET file_path=!file_path:~6!
                                     SET file_path=!file_path:/=\!)
        IF "%%b" EQU "SI_FILES" CALL :Get_File_Ext "%%c"
        IF "%%b" EQU "SI_CREATION"  (
            :Split date and time
            FOR /F "tokens=1*" %%d IN ("%%c") DO (
                :Split date
                FOR /F "tokens=1-3 delims=/" %%f IN ("%%d") DO (
                    SET file_month=0%%f
                    SET file_month=!file_month:~-2!
                    SET file_date=0%%g
                    SET file_date=!file_date:~-2!
                    SET file_year=20%%h)
                :Split time
                FOR /F "tokens=1-4 delims=: " %%i IN ("%%e") DO (
                    SET file_hour=%%i
                    SET file_minute=%%j
                    SET file_second=%%k
                    :Set 24 hour clock
                    IF "%%l" EQU "PM" (
                        SET /A file_hour=!file_hour!+12)
                    SET file_hour=0!file_hour!
                    SET file_hour=!file_hour:~-2!))))
    ECHO.Variables created for "%%a"
    ECHO.file_title     = !file_title!
    ECHO.file_path      = !file_path!
    ECHO.file_name      = !file_name!
    ECHO.file_extension = !file_extension!
    ECHO.file_year      = !file_year!
    ECHO.file_month     = !file_month!
    ECHO.file_date      = !file_date!
    ECHO.file_hour      = !file_hour!
    ECHO.file_minute    = !file_minute!
    ECHO.file_second    = !file_second!
    ECHO.
)
EXIT /B

:Get_File_Ext
SET file_name=%~n1
SET file_extension=%~x1
GOTO :EOF

[/code]

I'll leave it up to you to put them together and do actions with
them! i.e., !file_hour!:!file_minute!:!file_second!

It's usually not my practice to stack closing parens, but there are quite a few loops involved, and I did so to save space.

Insomniac at large


Report •

#32
June 22, 2010 at 20:29:14
thanks orangeboy!

it really helps and run successfully :)

but i wonder if the time is for example 12-34-21 PM..

i did run the code and the result is : 24-34-21

need your help to correct my code below that i have edited in order for the system to read the time in 12 PM to be 00

FOR /F "tokens=1-4 delims=: " %%i IN ("%%e") DO (
SET file_hour=%%i
SET file_minute=%%j
SET file_second=%%k

IF "%%l" EQU "PM" (
SET /A file_hour=!file_hour!+12

IF "%%i" EQU "12"(

SET file_hour=00%%l
SET file_hour=!file_hour:~2!)
)

SET file_hour=0!file_hour!
SET file_hour=!file_hour:~-2! )

SET file_time=!file_hour!-!file_minute!-!file_second!

)

i got message " the syntax is not correct"


Report •

#33
June 22, 2010 at 20:55:17
Yep. Yuck. Another condition is required. Accomplished a
couple of ways:

                    :Set 24 hour clock
                    IF "%%l" EQU "PM" (
                        IF NOT !file_hour! EQU 12 (
                            SET /A file_hour=!file_hour!+12))

or

                    :Set 24 hour clock
                    IF "%%l" EQU "PM" (
                        IF !file_hour! NEQ 12 (
                            SET /A file_hour=!file_hour!+12))

Note the second closing paren ")" for the second IF statement.

It's the little things that get ya...

Insomniac at large


Report •

#34
June 22, 2010 at 22:09:09
the code works fine..but..
let say the input time is:

12-34-44 AM
12-34-44 PM

both time will be output as 12-34-44... thus, it will assume both time as the same time which is 12-34-44 AM..

That is why, i planned to add some script in the script so that for 12-34-44 PM it will convert the time to 00-34-44 instead of 12-34-44..

but when i run the code, nothing happened..the time for 12-34-44 PM still 12-34-44 instead of 00-34-44


IF "%%l" EQU "PM" (
IF !file_hour! NEQ 12 (
SET /A file_hour=!file_hour!+12)

IF !file_hour! EQU 12 (
: i want to set the hour to be 00
SET /A file_hour=00%%i
SET file_hour=!file_hour!
)
)


Report •

#35
June 22, 2010 at 22:48:31
gotcha!


IF "%%l" EQU "PM" (
IF !file_hour! NEQ 12 (
SET /A file_hour=!file_hour!+12))

IF "%%l" EQU "AM" (
IF !file_hour! EQU 12 (
SET /A file_hour=!file_hour!-12))



i successfully got the script!
thanks my orangeboy!


Report •

#36
June 23, 2010 at 00:16:02
don't you mean AM?

IF "%%l" EQU "PM" (
IF !file_hour! NEQ 12 (
SET /A file_hour=!file_hour!+12)

IF !file_hour! EQU 12 (
:: here, it is 12PM, which is not what you want i don't think...
:: since 12PM is 12:. 12AM is 00.
: i want to set the hour to be 00
SET /A file_hour=00%%i
SET file_hour=!file_hour!
)
)

it gets confusing, but i think ya got yer AM/PM crossed up.

here would be my tentative stab:
set adj=%%I
set %adj:PM=12%
set /a file_hour+=adj
set /a test="file_hour%%12"
if !test! equ 0 set /a file_hour-=12
::----- end snippet

crappy, not refined, and NOT tested!
g'night


Report •

#37
July 7, 2010 at 01:20:28
Hi Orangeboy!

hope you still remember me..

thanks a lot for guiding me on batch scripting..

really appreciate it..thanks again!


Report •

#38
July 7, 2010 at 20:27:01
orangeboy, need your help again..

I did my script to backup daily files.But the script need to be run manually.Thus, I'm thinking of to do it run automatically in windows background. But if the script run automatically, it will not able to monitor the process either the files copied is successful or not. So, i need a technique that can log not just what the batch scripts are trying to do, but what the results are..It will output this to the log file, and you know the net group command succeeded. However, if there were errors, it will output, “The command completed with one or more errors.”

Below are my scripts..

@echo off

:: drive = destination %1 , source file = %2

set drive="%1"
set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "%2" "%drive%"

@pause

I really don't have any idea to make it run automatically.

I did some reference: To log the result of this, I might try:

e.g:
net group MyGroup joe /add >> AddAccts.log

i got confused on this..can someone please guide and explain it to me clearly?


really need your guidance,
thanks.


Report •

#39
July 7, 2010 at 22:00:25
You could add the following right after @echo off:

IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1
SHIFT

Basically what is happening is when the script runs, it looks
for /go as it's first argument. If /go is not the first argument, it
executes the script (represented by %0), and passes /go and
any other arguments to the new instance, and logs the results
(both good and bad) to the script's name.results.txt. Now
whenever the script runs, the results will get captured.

As far as automatically running, Window's Task Scheduler
should be able to take care of that.

Insomniac at large


Report •

#40
July 7, 2010 at 23:57:43
Thanks for the reply :)

the script works fine..but i wonder if there is any error in the process of backing up all files.. in my understanding,let say the user mistakenly type the source name wrongly.. (if mistakenly type the destination name,it will create new folder, thus there's no big deal)

I did try an error, for example i intentionally set wrong source name to run the backup process and the result that i received from .results.txt is " 0 file(s) copied " ... there are 2 meaning of "0 file(s) copied in this situation which is first, there are no file(s)/folder(s) that have been edited or additional files to be backed up and second, the files does not exist in the source (wrong source name assigned by the user)
..Thus i'm trying to upgrade the script in order for the script to differentiate between no new file(s) to be backed up and user wrongly insert wrong source name and print the result in log.. but it seems that i don't really understand the logic on how to manipulate the script and to output it as log.. (.results.txt)

fyi,
i did also try an error on the script and there are also other ways to output the result in log.Just wanna share with you :)

@echo off

set drive="C:\destination"
set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\source" "%drive%" >>.results.txt

@pause


thanks :)


Report •

#41
July 8, 2010 at 18:48:20
orangeboy, below are my script that i have edited..

@echo off

echo %date% %time% >>.results.txt

if exist "C:\Source" (
echo: File Exists
)

else (
echo: File Does Not Exist >>"C:\Results.txt")

IF NOT %1!==/go! %0 /go %* 1>%0.results.txt 2>&1
SHIFT

echo: >>.results.txt

set drive="C:\Destination"

set backupcmd=xcopy /s /d /e /h /i /f /y

%backupcmd% "C:\Source" "%drive%"


@paused

the script works fine except it doesn't print out the if statement either the file exist or not..

i wanted the output if there is no file to be copied like this;

1) if there is no new or updated file(s) to be copied:

0 file(s) copied
file exist

2) if there is wrong source folder:

0 file(s) copied
file does not exist

any idea?


Report •


Ask Question