Jump to content

How to merge two text files?


Recommended Posts

I have a fixed list of files, let's say:

1.dll

2.dll

3.dll

...

100. dll (all filenames are different, here it's just an example)

I would like to copy each of them only if the file exists. Is there any better way than doing it like this?

IF EXIST 1.dll COPY 1.dll <destination>

IF EXIST 2.dll COPY 2.dll <destination>

etc.

Edited by tomasz86
Link to comment
Share on other sites


I have a fixed list of files, let's say:

1.dll

2.dll

3.dll

...

100. dll (all filenames are different, here it's just an example)

I would like to copy each of them only if the file exists. Is there any better way than doing it like this?

IF EXIST 1.dll COPY 1.dll <destination>

IF EXIST 2.dll COPY 2.dll <destination>

etc.

Is the <destination> always the same?

Or can you use a "configuration file" (like a CSV)?

LIke:

1.dll ,"C:\Windows\Whatever\"
2.dll,"C:\Somewhere Else\"
....
<source file>,<target destnation>

You do a FOR loop:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
FOR /F "tokens=1,2 delims=," %%A IN (Files_to_copy.csv) DO (
ECHO IF EXIST %%A COPY /B %%A %%B
)

jaclaz

Link to comment
Share on other sites

I have a fixed list of files, let's say:

1.dll

2.dll

3.dll

...

100. dll (all filenames are different, here it's just an example)

I would like to copy each of them only if the file exists. Is there any better way than doing it like this?

IF EXIST 1.dll COPY 1.dll <destination>

IF EXIST 2.dll COPY 2.dll <destination>

etc.

I'm intrigued as to what you feel needs improvement in the samples you've provided.

You cannot copy what doesn't exist!

Link to comment
Share on other sites

Yzöwl,

I have a list of files (hundreds of files), ex.


  • _DEFAULT.PI_
    0401.CS_
    0404.CS_
    0405.CS_
    0406.CS_
    0407.CS_
    0408.CS_
    040B.CS_
    040C.CS_
    040D.CS_
    040E.CS_
    0410.CS_
    0411.CS_
    0412.CS_
    0413.CS_
    0414.CS_
    0415.CS_
    0416.CS_
    0419.CS_
    041D.CS_
    041F.CS_
    0804.CS_
    0816.CS_
    0C0A.CS_
    12520437.CP_
    12520850.CP_
    1394.IN_
    1394BUS.SY_
    192.DN_
    2195.138.1.BLD
    3DGARRO.CU_
    3DGMOVE.CU_
    3DGNESW.CU_
    3DGNO.CU_
    3DGNS.CU_
    3DGNWSE.CU_
    3DGWE.CU_
    3DSMOVE.CU_
    3DSNS.CU_
    3DSNWSE.CU_
    3DWARRO.CU_
    3DWMOVE.CU_
    3DWNESW.CU_
    3DWNO.CU_
    3DWNS.CU_
    3DWNWSE.CU_
    3DWWE.CU_
    8514FIX.FO_
    8514FIXE.FO_
    8514FIXG.FO_
    8514FIXR.FO_
    8514FIXT.FO_
    8514OEM.FO_
    8514OEME.FO_
    8514OEMG.FO_
    8514OEMR.FO_
    8514OEMT.FO_
    8514SYS.FO_
    8514SYSE.FO_
    8514SYSG.FO_
    8514SYSR.FO_
    8514SYST.FO_
    85775.FO_
    85855.FO_
    85F1257.FO_
    85S1257.FO_
    AAAAMON.DL_
    ABP480N5.SY_
    ACC_DIS.CH_
    ACCESS.CH_

Changing everything manually to

IF EXIST _DEFAULT.PI_ COPY _DEFAULT.PI_ <destination>
IF EXIST 0401.CS_ COPY 0401.CS_ <destination>

takes too much time. If I do just

COPY _DEFAULT.PI_ <destination>
COPY 0401.CS_ <destination>

then there is a "file not found" error on the screen (when the file doesn't exist) and the script is also much slower as it tries to copy all these files one by one.

Edited by tomasz86
Link to comment
Share on other sites

Changing everything manually to

The WHOLE idea about batch files is that you DO NOT change hundreds of lines manually!

That's ALSO exactly the ide of having a "configuration file".

You have same command line (which you change just once) and a LIST of files (source/destination) that it's much easier to create or change.

then there is a "file not found" error on the screen (when the file doesn't exist) ....

Meet REDIRECTION of standard output and standard error ;):

http://www.robvanderwoude.com/redirection.php

Also, please, ALWAYS use COPY /B when copying files, you never know:

http://ss64.com/nt/copy.html

This:

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET DESTINATION=C:\somewhere\
ECHO.>newfile.cmd
FOR /F "tokens=* delims=" %%A IN (list_of_dlls.txt) DO (
ECHO IF EXIST %%A COPY /B %%A %DESTINATION%>>newfile.cmd
)

Will create a new file "newfile.cmd" with all the entries in your list of dll's "list_of_dlls.txt" automatically (as opposed to manually)

jaclaz

Edited by jaclaz
Link to comment
Share on other sites

Using COPY /B serves no purpose when used in that manner, in fact it is the default mode when copying single files.

Are you sure?

The referenced page says the opposite. :unsure:

http://ss64.com/nt/copy.html

Maybe it changed between 2K and XP?

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/copy.mspx?mfr=true

jaclaz

Link to comment
Share on other sites

Are you saying that pre-XP the resulting file copied, C:\MyFiles\Binary.ext, as a result of the following doesn't work or is somehow broken?

IF NOT EXIST C:\MYFILES\ MD C:\MYFILES
COPY A:\BINARY.EXT C:\MYFILES

Link to comment
Share on other sites

Are you saying that pre-XP the resulting file copied, C:\MyFiles\Binary.ext, as a result of the following doesn't work or is somehow broken?

IF NOT EXIST C:\MYFILES\ MD C:\MYFILES

NO, I am not.

I am saying what I said (and nothing else) ;).

I have always used /B (probably uneededly :blushing: ) because of the referenced page AND along the lines of "better be safe than sorry" philosophy.

Since it is common that the good MS guys almost silently change some little things in commands and their syntax, [b]if[/b] the behaviour in earlier version was different [b]then[/b] everything would make sense, or if you prefer I was trying to find an explanation why the SS64 site (normally very exact) carried some wrong info.

jaclaz

Link to comment
Share on other sites

@ECHO OFF
SETLOCAL ENABLEEXTENSIONS
SET DESTINATION=C:\somewhere\
ECHO.>newfile.cmd
FOR /F "tokens=* delims=" %%A IN (list_of_dlls.txt) DO (
ECHO IF EXIST %%A COPY /B %%A %DESTINATION%>>newfile.cmd
)

In the end I used this method as a basis:


SETLOCAL ENABLEEXTENSIONS
SET A=
SET B=
SET C=
FOR /F %%I IN ('DIR/A-D/B %A%') DO (
IF EXIST %B%\%%I (
COPY/B %B%\%%I %C%\
)
)

I also have another question related to this script. Is it possible to copy only specific files like "*.*_"? I tried adding


IF "%%~xI"==".*_"

but it doesn't work. It works only if the extension is specified, ex.


IF "%%~xI"==".dl_"

Nevermind, I got it work by changing

('DIR/A-D/B %A%')

to

('DIR/A-D/B %A%\*.*_')

Everything is fine but copying is very slow compared to just doing xcopy :}

Edited by tomasz86
Link to comment
Share on other sites

It really depends upon the full intent of your script. You may be able to simply change the For command Dir output.

You need to be aware that asking for small changes throughout the Topic can completely ruin the final result. If you actually provide a full and complete explanation of the batch file requirements from the outset it is likely that the solution will better suit your intent.

As a side note, a file extension with an underscore suffix does not necessarily mean that the file is compressed.

Link to comment
Share on other sites

Yzöwl,

the script is connected with the other one for merging Win2k's updates ("merging text files..." topic). It's supposed to update files from Service Pack 4 with newer versions that come from the merged updates.

I already prepared it and it's working so I don't really need more help in this topic ;) I used strings like this one and that's why I asked for help.

	FOR /F %%I IN ('DIR/A-D/B SPMER\i386\*.*_') DO (
EXPAND -R SPMER\i386\%%I TEMP\ >NUL
FOR /F %%I IN ('DIR/A-D/B TEMP') DO (
ECHO Checking i386\%%I
IF EXIST HFMER\%%I (
ECHO Processing i386\%%I&ECHO.
XCOPY/DY HFMER\%%I TEMP\ >NUL&DEL/Q HFMER\%%I >NUL
MAKECAB /D CompressionMemory=21 /D CompressionType=LZX TEMP\%%I /L SPMER\i386 >NUL
)
DEL/Q/S TEMP\%%I >NUL
)
)

I have also rewritten everything in the other script for merging updates so now it's simpler and doesn't use any other (HFSLIP etc.) sources except for the parts where jaclaz's scripts are used. I'll post an updated version in the other topic after I combine both together as now they are two separate scripts.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...