Solved Export path based on presence of file

Dell / Latitude e6420
January 29, 2014 at 13:31:33
Specs: Win 7 Pro x64, I7 Sandy Bridge 2.201 GHz / 8072 MB
I need assistance in exporting some paths from the following network share.

G:\CustomerName(varies)\PartNumber(Varies)\Data\Operations\Setup

In the setup folder there will sometimes be a Word document. I need to list the path to each Setup folder that contains a Word document, skipping the empty ones. I would like to pipe this output to a text file. What would be the easiest way to do this? There would have to be wildcards for the customername and partnumber folders because those names will be different. The rest of the path will be the same for all customers. I don't really know any programming languages other than HTML. Maybe this could be done in batch or powershell?

~winipcfg

ASCII Question, Get an ANSI


See More: Export path based on presence of file

Report •

✔ Best Answer
January 30, 2014 at 20:11:55
Thanks Razor! I stand corrected on forfiles, since I do continue with XP. Comments in color, no less!
@OP, As for query #1, if the dir. structure is topoligically consistant, you need change only the value of the constant "data\operations\setup", but if they're further down or up the directory tree, then yes, you need to modify the tokens in line 5 to reflect that.
And for #2, as Razor implied, you should be able to just change *.doc to *.doc?
Also, since it was late, and I was tired, I didn't do much "detail work", such as changing to or otherwise accounting for "G:\". I will try to address all these items:
@echo off & setlocal
::----- dynamic section: change foll lines as needed
set base=G:\
set copydest=c:\x\archive
:: foll. 3 lines must be kept consistant and in synch
set marker=data\operations\setup
set range=4-6
set xx="%%c\%%d\%%e"
::==== end dynamic, begin static section main code
:: the foll line just isolates "setup" from the end of the marker-path-string
for %%a in (%marker%) do set end=%%~nxa
pushd %base%
(
for /f "tokens=*" %%a in ('dir /b /ad /s "%end%"') do (
  for /f "tokens=*" %%b in ('dir /b /s "%%a"\*.doc? 2^>nul') do (
    for /f "tokens=%range% delims=\" %%c in ("%%b") do (
      if /i %xx% equ "%marker%" echo copy "%%b" "%copydest%"
    )
  )
)
)>c:output.bat
popd
type output.bat
::====== end script

Note that nothing takes place other than the creation of a "slave" batch file, in current working directory of the primary drive, which is not executed. This allows you to pre-scan it for errors. It will be created in your original working directory.
For your edification concerning the content above, from cmd prompt type: FOR /?
edited to fix a couple of errors, for the record.

message edited by nbrane



#1
January 29, 2014 at 21:08:57
@echo off & setlocal
(
for /f "tokens=*" %%a in ('dir /b /ad /s setup') do (
for /f %%b in ('dir /b /s "%%a"\*.doc 2^>nul') do (
for /f "tokens=4,5,6 delims=\" %%c in ("%%b") do (
if /i "%%c\%%d\%%e" equ "data\operations\setup" echo %%b
)
)
)
)>output.txt
::====== end script
Awkward, but then, that's batch for ya. Not sure if the .DOC filename (word document) is constant or not, so i just tried to include all of them. "Forfiles" (3rd party) can probably handle this better, but I don't know it well enough.
I may have screwed up the paths with the spaces, which I'm sure are present and f(oul)ing things up as usual. I'll wait for further developments before correcting.

message edited by nbrane


Report •

#2
January 30, 2014 at 04:53:08
Hi Nbrane,
Thank you for the code. Is there any chance that you might add some comments into this batch file so that I know what it's doing? Maybe I can learn a little something along the way :-)

Edit: I haven't ran this yet because I'm not sure what it's doing.
Edit2: Yes unfortunately customer and part# sometimes contain spaces
~winipcfg

ASCII Question, Get an ANSI

message edited by winipcfg


Report •

#3
January 30, 2014 at 07:17:50
Untested PowerShell looks something like this:
Get-ChildItem G:\ -Filter "CustomerName*" |
Get-ChildItem -Filter "PartNumber*" | 
Where-Object { Test-Path "$($_.FullName)\Data\Operations\Setup\*.doc*" } | 
Select-Object { "$($_.FullName)\Data\Operations\Setup" } |
Tee-Object "someFile.txt"

How To Ask Questions The Smart Way

message edited by Razor2.3


Report •

Related Solutions

#4
January 30, 2014 at 07:24:55
FORFILES was included with Windows starting with Vista, so it's first party unless you're still rockin' XP.
@echo off & setlocal <-- Hide our work
(
for /f "tokens=*" %%a in ('dir /b /ad /s setup') do ( <-- Look for any directories named "setup"
for /f %%b in ('dir /b /s "%%a"\*.doc 2^>nul') do ( <-- Look for files ending in ".doc"
for /f "tokens=4,5,6 delims=\" %%c in ("%%b") do ( <-- Make sure there's a "data\operations\setup" between here and the ".doc" file
if /i "%%c\%%d\%%e" equ "data\operations\setup" echo %%b <-- If there is, print the ".doc" file's path
)
)
)
)>output.txt <-- Dump to file "output.txt"

How To Ask Questions The Smart Way


Report •

#5
January 30, 2014 at 10:25:56
I'm running this right now. Could take quite awhile to run.
Two questions:
1.) If I want to change the path for which this batch searches would I need to just change the path in the following line"
if /i "%%c\%%d\%%e" equ "data\operations\setup" echo %%b <-- If there is, print the ".doc" file's path
I could be way off because I don't understand the for loops with the tokens & delims settings.

2.) If I wanted to search for both .doc and .docx files would I need 2 separate scripts or this be modified to include both file extensions?

~winipcfg

ASCII Question, Get an ANSI


Report •

#6
January 30, 2014 at 10:29:35
1) You'd have to do a little more work than that. Namely, you'd have to determine the absolute position in the path you want to test against, and modify both that line, and the previous FOR line. The PowerShell script is more/less work, depending.

2) Do what I do in the PowerShell script; look for "*.doc*" instead of just "*.doc"

How To Ask Questions The Smart Way


Report •

#7
January 30, 2014 at 20:11:55
✔ Best Answer
Thanks Razor! I stand corrected on forfiles, since I do continue with XP. Comments in color, no less!
@OP, As for query #1, if the dir. structure is topoligically consistant, you need change only the value of the constant "data\operations\setup", but if they're further down or up the directory tree, then yes, you need to modify the tokens in line 5 to reflect that.
And for #2, as Razor implied, you should be able to just change *.doc to *.doc?
Also, since it was late, and I was tired, I didn't do much "detail work", such as changing to or otherwise accounting for "G:\". I will try to address all these items:
@echo off & setlocal
::----- dynamic section: change foll lines as needed
set base=G:\
set copydest=c:\x\archive
:: foll. 3 lines must be kept consistant and in synch
set marker=data\operations\setup
set range=4-6
set xx="%%c\%%d\%%e"
::==== end dynamic, begin static section main code
:: the foll line just isolates "setup" from the end of the marker-path-string
for %%a in (%marker%) do set end=%%~nxa
pushd %base%
(
for /f "tokens=*" %%a in ('dir /b /ad /s "%end%"') do (
  for /f "tokens=*" %%b in ('dir /b /s "%%a"\*.doc? 2^>nul') do (
    for /f "tokens=%range% delims=\" %%c in ("%%b") do (
      if /i %xx% equ "%marker%" echo copy "%%b" "%copydest%"
    )
  )
)
)>c:output.bat
popd
type output.bat
::====== end script

Note that nothing takes place other than the creation of a "slave" batch file, in current working directory of the primary drive, which is not executed. This allows you to pre-scan it for errors. It will be created in your original working directory.
For your edification concerning the content above, from cmd prompt type: FOR /?
edited to fix a couple of errors, for the record.

message edited by nbrane


Report •

Ask Question