Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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.txti want to move any one of the above files to folder b
acc.txt or xtr.txtif folder a is having only one file
folder A
acc.txtthen 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

@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_BatchYour homework is done, but tomorrow at work?

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

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

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

@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?

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

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 EnableDelayedExpansionfor %%D in (C:\"Your Folder") do (
for /f "tokens=* delims= " %%A in ('dir /b/s %%D\*.txt') do (
echo copy %%A C:\"Your Folder"
)
)
pauseTake out the echo and pause if what you need.
Else wait for IVO as he is very knowledgeable!

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

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.

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

Hello Mechanix,
In folder A there will be only two files at all times...min 1.txt file, max 2.txt filesfrom 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

@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

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

@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

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
)

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |