Computing.Net > Forums > Programming > Move 1.txt file to other folder and

Move 1.txt file to other folder and

Reply to Message Icon

Original Message
Name: padma_priya
Date: June 10, 2008 at 01:04:14 Pacific
Subject: Move 1.txt file to other folder and
OS: xp
CPU/Ram: intel core duo
Model/Manufacturer: 2006
Comment:

Hi All,

I requires a .bat script for the below requirement

i have two .txt files in a folder A , i want to move any one of these .txt to folder B.If in folder A i have only 1.txt file then i want to create a empty file by the same name as the file present in folder A

example:

folder A
acc.txt
xtr.txt

i want to move any one of the above files to folder b
acc.txt or xtr.txt

if folder a is having only one file

folder A
acc.txt

then i want to create same file in folder B but it should me empty

Please sugest me how can i do this?

Thanks,
Padma_priya


Report Offensive Message For Removal


Response Number 1
Name: IVO
Date: June 10, 2008 at 02:08:00 Pacific
Reply: (edit)

@echo off
if exist C:\FolderA\file1.txt if exist C:\FolderA\file2.txt (
copy C:\FolderA\file1.txt C:\FolderB
copy C:\FolderA\file2.txt C:\FolderB
) else (
if not exist C:\FolderA\file1.txt type nul > C:\FolderB\file1.txt
if not exist C:\FolderA\file2.txt type nul > C:\FolderB\file2.txt
)
:: End_Of_Batch

Your homework is done, but tomorrow at work?


Report Offensive Follow Up For Removal

Response Number 2
Name: NoIdea
Date: June 10, 2008 at 02:30:44 Pacific
Reply: (edit)

Padma_priya,
Just for my interest why do you want to copy and make blank/empty a txt file?


Report Offensive Follow Up For Removal

Response Number 3
Name: padma_priya
Date: June 10, 2008 at 02:34:49 Pacific
Reply: (edit)

NoIdea,

from my source system i may receive 1 or 2 files daily,if iam not creating an empty file then my BI routine will serach for the file and fails the job if the file is not present, so i want to create an empty file.

thx,
Padma_priya


Report Offensive Follow Up For Removal

Response Number 4
Name: IVO
Date: June 10, 2008 at 02:40:28 Pacific
Reply: (edit)

@NoIdea,

in my opinion that is just what qualifies the post as homework; the question looks like an exercise about AND/OR clause in if statements. So no other practical meaning, but a test to put people at thinking.


Report Offensive Follow Up For Removal

Response Number 5
Name: padma_priya
Date: June 10, 2008 at 02:47:12 Pacific
Reply: (edit)

Hi Ivo,

Thanks for the reply.

sorry for the confusion

I tried the batch script,but i will receive the file name as below

Acc_200802051125.txt
Acc_200803041258.txt

i need to move any one of these files to folder B from folder A
that can be latest file or old one any one file.
and if i have one file in folder A, then i have to create an empty file in folder b with same file name.

once agin sorry for the confusion.

Thanks,
Padma_priya


Report Offensive Follow Up For Removal


Response Number 6
Name: IVO
Date: June 10, 2008 at 03:02:53 Pacific
Reply: (edit)

@Padma_priya,

well that is not a homework and of course my solution doesn't work as it assumes the filenames are known in advance.

Please explain better if in the folder there are one or two files each day and if not (there are many) the rule to identify those of interest. More do you want to copy or move the files from the source folder?


Report Offensive Follow Up For Removal

Response Number 7
Name: padma_priya
Date: June 10, 2008 at 04:21:43 Pacific
Reply: (edit)

Hello Ivo,

Thanks for your quick response.

i will get my source files delivered in folder A in below format
Acc_200802051125.txt
Acc_200803041258.txt

i have a etl routine which will process the job..but the etl routine will fail if it finds more than one file...so i want to move the second file completely to folder B...

if i have only one file in folder A then i want to create a empty file in folder b with the same filename as in folder A

why i want an empty file to be created because. I may receive one or two files daily....if my etl routine cannot find a file in any of these two folders it will fail so i want to create a empty file with same name as in folder A.


And In folder A i will have only .txt files ..i want to move the file from folder A to folder B.

please let me know if you have still have any doubt..

Thanks,
Padma_priya


Report Offensive Follow Up For Removal

Response Number 8
Name: NoIdea
Date: June 10, 2008 at 04:23:25 Pacific
Reply: (edit)

Padma_priya,
Since it looks like you do not need to empty the text files just need something there. Empty or not. Perhaps this?

@echo off
setLocal EnableDelayedExpansion

for %%D in (C:\"Your Folder") do (
for /f "tokens=* delims= " %%A in ('dir /b/s %%D\*.txt') do (
echo copy %%A C:\"Your Folder"
)
)
pause

Take out the echo and pause if what you need.
Else wait for IVO as he is very knowledgeable!


Report Offensive Follow Up For Removal

Response Number 9
Name: sunitha_1984
Date: June 10, 2008 at 05:28:33 Pacific
Reply: (edit)

Padma,

These kind of things cannot be done in dos scripting...you need to do shell scripting kind of thing.

Thanks,
Sunitha


Report Offensive Follow Up For Removal

Response Number 10
Name: klint
Date: June 10, 2008 at 05:40:23 Pacific
Reply: (edit)

Sunitha,

We are not talking DOS, this is XP and its command language is much more powerful and certainly capable of doing this task.

In my previous post on another thread here, I said that Windows batch language was not very good at reading files line-by-line. But in this thread, we are not reading the files, we are doing things with them at file level rather than content level.


Report Offensive Follow Up For Removal

Response Number 11
Name: Mechanix2Go
Date: June 10, 2008 at 05:57:19 Pacific
Reply: (edit)

As usual, a solution starts ith a coherent problem statement.

I still don't know whether ANY means 'any one; doesn't matter which' or 'all files'.

And does MOVE mean move? Or COPY?

And in which folder does the absence of a second file goof up the process?


=====================================
If at first you don't succeed, you're about average.

M2


Report Offensive Follow Up For Removal

Response Number 12
Name: padma_priya
Date: June 10, 2008 at 07:44:57 Pacific
Reply: (edit)

Hello Mechanix,


In folder A there will be only two files at all times...min 1.txt file, max 2.txt files

from folder A any one file i need to move...it can be first file or second file any file there is no order that i need to move first file only...i need to have only one file that can be any one of two files.

And i need to completely move the file from folder A to folder B.

and if i have only one file in folder a i need to create an empty file with same filename as in folder A.

Hope this is clear

Thanks,
Sunitha


Report Offensive Follow Up For Removal

Response Number 13
Name: IVO
Date: June 10, 2008 at 09:51:28 Pacific
Reply: (edit)

@echo off
copy C:\FolderA\*.txt C:\FolderB
set counter=0
for %%j in (C:\FolderA\*.txt) do set /A counter+=1
if %counter% equ 2 goto :EOF
for %%j in (C:\FolderA\*.txt) do type nul > C:\FolderB\%%~nxj
:: End_Of_Batch


Report Offensive Follow Up For Removal

Response Number 14
Name: padma_priya
Date: June 10, 2008 at 10:10:52 Pacific
Reply: (edit)

Hi IVO,

Thanks for the script.

I have executed the script, it is working fine for the second case only i.e if i have only one txt file in folder A,it is creating a empty file with the same name in folder B.

But the script is failing for the first case, if i have two txt files,it is coping these two txt files to folder B.
But what i required is I need to MOVE any one of the txt file to folder B.

Thanks,
Padma


Report Offensive Follow Up For Removal

Response Number 15
Name: IVO
Date: June 10, 2008 at 12:18:22 Pacific
Reply: (edit)

@echo off
copy C:\FolderA\*.txt C:\FolderB
set counter=0
for %%j in (C:\FolderA\*.txt) do set /A counter+=1
if %counter% equ 2 (
del C:\FolderA\*.txt
) else (
for %%j in (C:\FolderA\*.txt) do type nul > C:\FolderB\%%~nxj
)
set counter=
:: End_Of_Batch



Report Offensive Follow Up For Removal

Response Number 16
Name: klint
Date: June 11, 2008 at 03:19:09 Pacific
Reply: (edit)

Here's my attempt (I've started with Ivo's script and modified it)
===========================================================
@echo off
setlocal enabledelayedexpansion
set counter=0
for %%j in (C:\FolderA\*.txt) do (
set /A counter+=1
if !counter! equ 2 (
move %%j C:\FolderB
)
)
if %counter% equ 1 (
for %%j in (C:\FolderA\*.txt) do type nul > C:\FolderB\%%~nxj
)


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Move 1.txt file to other folder and

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 5 Days.
Discuss in The Lounge