?
Solved

Batch file to find files that were are missing

Posted on 2012-08-26
12
Medium Priority
?
798 Views
Last Modified: 2012-09-01
Hi,

I have 1 main folder (called "MyFolder") with 10 sub-folders with 500000 files each.

Each of these sub-folders has 2 types of files:
1. file with ext .txt
2. file with ext .key

Each .txt file must have a corresponding .key file

e.g. a.txt should have a.key

But I know there are some .txt files who do not have a .key file.

I need to find those .txt files and move them to a folder called "NoLink".

Can you help me with a bat file that would search it and move those non-linked files to the NoLink folder?
0
Comment
Question by:nainil
  • 5
  • 3
  • 3
  • +1
12 Comments
 
LVL 85

Expert Comment

by:oBdA
ID: 38335618
Try this; it currently moves the files without key files into a flat folder, so if there are duplicate names, the last one will win. If you want the source structure recreated in the "NoLink" folder, say so. The script is currently in test mode, it will only show the commands it would normally run; remove the uppercase ECHO to run it for real.
I don't dare making any assumptions about the processing speed, though ...
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp
set TargetFolder=C:\NoLink
if not exist "%TargetFolder%" md "%TargetFolder%"
set Mask=*.txt
for /r "%SourceFolder%" %%a in (%Mask%) do (
	echo Processing %%a ...
	if exist "%%~dpna.key" (
		echo ... key file found.
	) else (
		echo ... key file NOT found, moving ...
		ECHO move "%%~a" "%TargetFolder%"
	)
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38336007
nainil

Best to do it like this.

If there are files with the same names but in different folders then it's best to keep them seperate by recreating their paths in 'NoLink'

Also, by recreating a TXT-file's path in 'NoLink', it's easier to identify where it originally came from.

NOTE: Change lines 2 and 3 to point to your own folders.

@echo off
set source=c:\MyFolder
set destination=c:\NoLink

for /d %%a in ("%source%\*") do (
  md "%destination%\%%~na" 2>nul
  for %%b in ("%%a\*.txt") do if not exist "%%~pnb.key" move "%%b" "%destination%\%%~na"
)

for /d %%a in ("%destination%\*") do if not exist "%%a\*.txt" rd "%%a"

Open in new window

0
 
LVL 11

Assisted Solution

by:paultomasi
paultomasi earned 500 total points
ID: 38336017
nainil

Unlike my first program above, if you don't need to recreate the folder structure in 'NoLink' then the following will do.

NOTE: Change 'c:\MyFolder' and 'c:\NoLink' in lines 3 and 5 respectively to point to your own folders if they are named differently.

@echo off

for /d %%a in ("c:\MyFolder\*") do (
  for %%b in ("%%a\*.txt") do (
    if not exist "%%~pnb.key" move "%%b" "c:\NoLink"
  )
)

Open in new window

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 11

Expert Comment

by:paultomasi
ID: 38336030
nainil

Just a thought, do the folders in 'MyFolder' contain subfolders or are the 500000 files in single (flat) folders?
0
 
LVL 10

Expert Comment

by:ReneGe
ID: 38337105
Hey oBdA and Paul :)

@nainil

Coulkd you please confirm that your directory structure is like follows?

C:\MyFolder\SubDir01\
C:\MyFolder\SubDir02\
...
C:\MyFolder\SubDir10\

Also, please confirm if their may be some .txt files that have the same name among all your 500 000 .txt files.

Cheers,
Rene
0
 

Author Comment

by:nainil
ID: 38337426
@Rene : All your questions have the same answer. Yes.
0
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 38337466
Then this will recreate the original folder structure under the "NoLink" folder. It's in test mode and will only display the directory creation and move commands it would normally run. remove the three uppercase "ECHO"s to run it for real:
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Temp
set TargetFolder=C:\NoLink
ECHO if not exist "%TargetFolder%" md "%TargetFolder%"
set Mask=*.txt
for /r "%SourceFolder%" %%a in (%Mask%) do (
	echo Processing %%a ...
	if exist "%%~dpna.key" (
		echo ... key file found.
	) else (
		echo ... key file NOT found, moving ...
		if not exist "%TargetFolder%%%~pa" (
			ECHO md "%TargetFolder%%%~pa"
		)
		ECHO move "%%~a" "%TargetFolder%%%~pa"
	)
)

Open in new window

0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38337646
nainil

You overlooked a vital question http:#a38336030.
0
 

Author Comment

by:nainil
ID: 38348652
Just a thought, do the folders in 'MyFolder' contain subfolders or are the 500000 files in single (flat) folders?

Answer: MyFolder contains sub folders and each of the sub folder contains 500000 files in them.
0
 

Author Comment

by:nainil
ID: 38348663
@oBDA:

When I run this code:
@echo off
setlocal enabledelayedexpansion
set SourceFolder=C:\Documents and Settings\nainil.HUB\Desktop\Archive
set TargetFolder=C:\Documents and Settings\nainil.HUB\Desktop\Archive_Completed
if not exist "%TargetFolder%" md "%TargetFolder%"
set Mask=*.txt
for /r "%SourceFolder%" %%a in (%Mask%) do (
	echo Processing %%a ...
	if exist "%%~dpna.key" (
		echo ... key file found.
	) else (
		echo ... key file NOT found, moving ...
		if not exist "%TargetFolder%%%~pa" (
			ECHO md "%TargetFolder%%%~pa"
		)
		move "%%~a" "%TargetFolder%%%~pa"
	)
)

Open in new window


I get the following error while moving. What am I doing wrong?

C:\Documents and Settings\nainil.HUB>"C:\Documents and Settings\nainil.HUB\Deskt
op\compare_files.bat"
Processing C:\Documents and Settings\nainil.HUB\Desktop\Archive\0d4afabbb4d811b2
d6a1c06ee201f8e1.txt ...
... key file found.
Processing C:\Documents and Settings\nainil.HUB\Desktop\Archive\0d6b97da35c51ba6
160d73941aee6e5d.txt ...
... key file found.
Processing C:\Documents and Settings\nainil.HUB\Desktop\Archive\0d8da6880b38f3c1
6a764390ee0c84aa.txt ...
... key file NOT found, moving ...
md "C:\Documents and Settings\nainil.HUB\Desktop\Archive_Completed\Documents and
 Settings\nainil.HUB\Desktop\Archive\"
The system cannot find the path specified.

Open in new window

0
 
LVL 85

Expert Comment

by:oBdA
ID: 38348959
You didn't remove the second of the three test "ECHO"s (the one in line 14) which creates the target directory if it doesn't exist. Remove that ECHO as well, and it should run just fine.
0
 
LVL 11

Expert Comment

by:paultomasi
ID: 38349488
nainil

A couple of issues...

I asked you a specific question at http:#a38336030 to which you did not give a specific answer at http:#a38348652

I know there are subfolders under MyFolder however, I wanted to know if there are subfolders within those subfolders.

I assume if you're running oBdA's 2nd code at http:#a38337466 then you have skipped past my code at http:#a38336007 (see below).

Oh, by the way, line 10 (from my original code) can be omitted because all it does is clear up empty subfolders under NoLink, so I've re-listed just the main guts of the program which does what you asked for.

@echo off
set source=c:\MyFolder
set destination=c:\NoLink

for /d %%a in ("%source%\*") do (
  md "%destination%\%%~na" 2>nul
  for %%b in ("%%a\*.txt") do if not exist "%%~pnb.key" move "%%b" "%destination%\%%~na"
)

Open in new window

0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to install and use the NTBackup utility that comes with Windows Server.
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question