Windows xp / batch get file info and change folder

The following in a batch xp sp3

I have a folder called c:\test
In this folder an other program is making files the first file is test1 then test2 etc
this file's allways uses the same layout but where i put the stars is variable:
name=*******   <--this can me any name not one word
order=******* <-- this can only be digits as one word
I need to see if a file exist in the folder and pipe tne name and the order to a text file and then to see if more files exist.

Thx in advance

Dan  


LVL 6
Danny KonAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
It's not clear what you are after, can you clarify further.  Will there be other files in the folder that you do not want to look for, or if there are any files in the folder do you want to process each one?  You mention the "name" and "order", but you don't mention how those relate to the file names?  Also, you show each with 7 astericks, does that mean each will be exactly 7 characters long?  So all filename will look like:

NNNNNNNOOOOOOO

If not, what is the exact format of the real files, with examples.  It sounds like you want to look for these files and then break apart the "name" and the "order" and then send those on for further processing, but it's not clear how a script would extract those two pieces from each filename.

~bp
0
Danny KonAuthor Commented:
Billpreww

No the folder can't have any other files. except for files lke test1.txt test2.txt test3.txt
Filenames has nothing do do with the " name" and "order" the files are as menioned test1, test2  the ext = txt
Furthermore the ordename are only digits but rising i think we are now at 1235755 so not 7 digits can be any number.
example:

"test3.txt"
Name=The Hardrock Cafe
order=1235755

Thx Danny
0
Bill PrewCommented:
See if this gets you close.  I added comments to try and give you and idea how it works.  You will need to change the two SET lines near the top to reference the directory where your files are located, and also the output file.  

~bp
@echo off
setlocal EnableDelayedExpansion
 
rem Define directory of files to process, and output file destination
set "BaseDir=c:\temp\EE25584769\files"
set OutputFile=c:\temp\EE25584769\output.txt
 
rem Loop through all files in the directory
for %%A in ("%BaseDir%\*.*") do (
  rem Read each line of this file
  for /f "usebackq tokens=*" %%B in ("%%A") do (
    set Line=%%B
    rem If it begins with "name=" or "order" number then get the value after the "=" sign into a variable of the same name
    if /i "name=" equ "!Line:~0,5!" set "!Line!"
    if /i "order=" equ "!Line:~0,6!" set "!Line!"
  )
  rem Append a line to the output file with this files name and data
  echo "%%A", "!name!", "!order!">>%OutputFile%
)

Open in new window

0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

AmazingTechCommented:
Make sure each line does not have a space at the end.
You can run this as many times as you want the OutputFile will only add items that were not there previously.

SETLOCAL ENABLEDELAYEDEXPANSION
Set BaseFolder=C:\test
Set OutputFile=DannyKon.txt

for /f "tokens=2,3,* delims=:=" %%a in ('findstr /i /c:"Name=" /c:"order=" "%BaseFolder%\*.txt"') do (
    If "!File!" NEQ "%%~na" (Set File=%%~na& Set Name=& Set Order=)
    Set %%b=%%c
    If defined name if defined order (
        find /i "!File!,!Name!,!Order!" "%OutputFile%">NUL 2>NUL
        if errorlevel 1 echo !File!,!Name!,!Order!>>"%OutputFile%"
    )
)
0
Danny KonAuthor Commented:
billprew:

Your program is giving me underneath as the result thats not correct  test1.cmd and test2.cmd do not having lines with hardrock cafe.
"c:\test\test.txt", "The Hardrock Cafe", "1235755"
"c:\test\test1.cmd", "The Hardrock Cafe", "1235755"
"c:\test\test1.txt", "The Hardrock Cafe", "123575545"
"c:\test\test2.cmd", "The Hardrock Cafe", "123575545"

AmazingTech:
test,The Hardrock Cafe,1235755
test1,The Hardrock Cafe,123575545

That's is allmost correct only i don't need the 2 lines test test1 i need 1 line
A other program is making the test test1 test2 test3 etc
I need the program to show 2 lines in your example
test.txt
DannyKon.txt
can only be the 2 lines
The Hardrock Cafe
1235755
Then to find the the next file but wait till the (DannyKon.txt)is deleted
Also it will be greet if it a checks will processing if there is a new test.txt test1.txt created by the other program so if there are the following files
test2.txt test3.txt test4.txt and the other program is making a new test.txt it will also be processed but but again only if the output file is deleted

Thx so much
Danny
0
AmazingTechCommented:
Can you show us the intended output?

I'm using DannyKon.txt as the output name.

So should DannyKon.txt:
The Hardrock Cafe,1235755
The Hardrock Cafe,123575545

or

The Hardrock Cafe
1235755
The Hardrock Cafe
123575545

or something else?

Like DannyKon.txt would have just 1 file. test.txt
The Hardrock Cafe
1235755

Wait until DannyKon.txt is deleted before putting test1.txt into DannyKon.txt
The Hardrock Cafe
123575545

and so on. Would test.txt and test1.txt be deleted?
0
Danny KonAuthor Commented:
AmazingTech:

The last thing what you say under:
or something else?

DannyKon.txt can only have two lines
The Hardrock Cafe
1235755

If the files DannyKon.txt is deleted by me

The following files is read to the  DannyKon.txt example  
Ricks cafe
4322344

Now the file your program read's can be deleted so my meaning if your program read
test.txt it will put the output to DannyKon.txt. If I delete DannyKon.txt it will check for the next file and put the output again to DannyKon.txt and it will delete the file it just read.

Again your help is very much appreciated

Danny
0
AmazingTechCommented:
OK. I changed it up a little bit:

SETLOCAL ENABLEDELAYEDEXPANSION
Set BaseFolder=C:\test
Set Processed=C:\Done
Set OutputFile=DannyKon.txt

for /f "tokens=1,2,3,* delims=:=" %%a in ('findstr /i /c:"Name=" /c:"order=" "%BaseFolder%\*.txt"') do (
    If "!File!" NEQ "%%~nb" (Set File=%%~nb& Set Name=& Set Order=)
    Set %%c=%%d
    If defined name if defined order CALL :WriteFile "!NAME!" "!ORDER!" "%%a:%%b"
)
goto :EOF

:WriteFile
ECHO %~1>"%OutputFile%"
ECHO %~2>>"%OutputFile%"

:WaitUntilDeleted
if exist "%OutputFile%" GOTO WaitUntilDeleted

move "%~3" "%Processed%\"
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Danny KonAuthor Commented:
AmazingTech:

This is 98%a right the only thing it doesnt do is that it deletes the input file and if there are no input files  your program has to stop processing

Danny
0
Bill PrewCommented:
Sorry guys, got wrapped up at the office today.  Okay, based on the additional requirements that have been given, and borrowing a few thoughts from AT, here's where I'm at.  See if this makes sense and meets your needs.

I added a "sleep" in the loop checking the file deletion, I think you will really want something like that otherwise cpu usage until you delete the file will be high.  I didn't see a request to archive the processed files, just delete them so that's the approach I took.  If I missed a need to move them then that can be added back.

Here's one location for a sleep utility from the Microsoft resource kit:

http://www.dynawell.com/download/reskit/microsoft/win2000/sleep.zip

AmazingTech, I'll be travelling for the next few days with lower access to email. I know you have your own approach going, but if see any merit in my approach and there are tweaks needed I have no problem with you lending a hand.  My goal is to help get the poster a solution (and have a little coding fun along the way), and where the points fall is secondary.

I also think we need to look for files with name= and order= from scratch after each one is processed and the working text file is deleted.  This is because new files could be added after the FOR loop starts, so doing the findstr once and then working through the result list could lead to a premature termination.

~bp
@echo off
setlocal EnableDelayedExpansion
 
set "BaseDir=c:\temp"
set "OutputFile=DannyKon.txt"
 
:MainLoop
  call :ProcessNextFile
  if "%FileProcessed%" == "" exit /b
:MainDelay
  sleep 5
  if exist "%OutputFile%" goto :MainDelay
  del "%FileProcessed%"
  goto :MainLoop
 
:ProcessNextFile
  set "FileProcessed="
  set "Name="
  set "Order="
  for /f "tokens=*" %%A in ('findstr /i /m "name= order=" "%BaseDir%\*.*"') do (
    for /f "tokens=*" %%B in ('findstr /i "name= order=" "%%~A"') do set "%%B"
    if defined Name if defined Order (
      echo !Name!>"%OutputFile%"
      echo !Order!>>"%OutputFile%"
      set "FileProcessed=%%~A"
      exit /b
    )
  )
  exit /b

Open in new window

0
AmazingTechCommented:
Hmm... I think it should do all that you want.

After test.txt is copied to DannyKon.txt and DannyKon.txt is deleted. test.txt should be moved to C:\Done and then continues to the next .txt file.

Can you explain what needs to be changed?
0
Danny KonAuthor Commented:
AmazingTech
The problem the program had was me ;)
I forget to make the target map c:\done
This was more then i can ask for, Great solutions
Billprew Thx for the great effort

Again Thx a lot  

Danny
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.