Jump to content

Recommended Posts

Posted

Hi,

Quick question.

Is it possible to search for long file names on the network? Some of our staff have a habit of pressing save rather than calling it a proper name. These can sometimes cause errors when doing backup to file (rarely).

Thanks

Posted (edited)

The script below searches the C: drive on the local computer for files and reports those where the full path is longer than 60 characters - adjust the "C:" and "60" as appropriate.

 

You can run this against a remote computer - just put its name in place of the "." but it would be quicker running it on the server.

 

If you're having problems backing up long files then there are two possible reasons for this; the software is c*&p or the user running the backup process does not have "backup privilege"

 

(Problems with long file names are typically because the server stores something as d:\home folders\year9\student\maths but the user sees that as n:\maths; amongst other things, backup privilege bypasses the length restrictions. If you're using robocopy to run the backup then use it with the /b switch to fix the problem)

sComputer="."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
set colItems = oWMIService.ExecQuery("select * from CIM_Datafile where drive='C:'")
for each item in ColItems
 if len(item.name)>60 then
   wscript.echo item.name
 end if
next

Edited by srochford
  • Thanks 1
Posted

Thanks Steve will give this a try this morning.

We backup with Windows backup to tape and never have any problems. But once a term of so we copy and paste all the home folders to a folder on a remote computer and it usually kicks up errors with long file names. I've since started using windows backup to backup to remote to the remote pc

Posted

Sorry; that's normally what causes the problem but you can test for the filename length (rather than full path) by checking the length of "filename" rather than "name" - code below. The echo statement shows the full path - you might need to know where the problem file is :-)

 

sComputer="."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
set colItems = oWMIService.ExecQuery("select * from CIM_Datafile where drive='C:'")
for each item in ColItems
 if len(item.filename)>6 then
   wscript.echo item.name, item.FileName
 end if
next

  • Thanks 1
Posted

Everything's possible :-)

 

The "execquery" line is using WQL (WMI Query Language; recursive acronyms are great!) and you can add more bits to it.

("select * from CIM_Datafile where drive='C:' and path='\\temp\\'")

will search c:\temp - note the doubled backslashes

 

You can also search for particular kinds of file - eg

("select * from CIM_Datafile where drive='C:' and path='\\temp\\' and extension='mp3'")

would search for MP3s under c:\temp and this would search for mp3 and avi in c:\home\year9:

("select * from CIM_Datafile where drive='C:' and path='\\home\\year9\\' and (extension='mp3' or extension='avi')")

watch out for brackets and single and double quotes!

Posted

Stupid question possibly, but I don't use this VB scripting much myself.

 

Will that keep the file extension, or lose it? IE will "mydumbfilenameisfartoolongtobeanypossibleuseforanythingatall.doc" become "mydumbfilename.doc" or "mydumbfilenameisfart" ?

Posted

Not sure I understand the question. The scripts I've shown won't change anything - they'll simply show you what's there. If you have the extremely long filename then that's what you'll see reported. It's then up to you to decide what to do with it.

 

It's possible for users to create files where the total length of the path on the physical drive plus the filename is too long for explorer etc to cope with but the user sees it with no problem because they start lower down the tree. Once you've found such a long file you can deal with it by just mapping a drive to the lower level folder; explorer will then let you rename the file (if that's what you want to do)

Posted

Steve,

Is the syntax of this correct? Don't seem to get any results back and i've put a test file in with a long name: I'm running it on the server and the path for home directories is D:\data\staff. Will it search through all sub-folders in there IE individual staff?

 

sComputer="."

Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")

set colItems = oWMIService.ExecQuery("select * from CIM_Datafile where drive='D:' and path='\\data\\staff\\'")

for each item in ColItems

if len(item.filename)>15 then

wscript.echo item.name, item.FileName

end if

next

Posted

Must be missing something but in this line of code

 

("select * from CIM_Datafile where drive='C:' and path='\\home\\year9\\' and (extension='mp3' or extension='avi')")

 

The temp path is not specified so how would that work ?

 

To me it looks like it would search

 

C:\home\year9

 

for mp3's and avi files.

  • 3 months later...
Posted (edited)

Microsoft's LogParser is another alternative. The following script works perfectly for me, although you may want to change a few things in it. :)

 

@echo off
SET PATH=%PATH%;"%ProgramFiles%\Log Parser 2.2"
LogParser.exe "SELECT Path, Size FROM D:\*.* WHERE STRLEN(Path) > 200" -i:FS -preserveLastAccTime:ON -o:CSV > Results.csv

See the Log Parser help file for more info (particularly the section called FS Input Format Fields). The amount of things you can do with Log Parser is amazing.

 

Btw, sorry for bumping an old thread but I thought it might be useful.

Edited by Arthur
  • Thanks 1
Posted

Btw, sorry for bumping an old thread but I thought it might be useful.

 

It is useful - learning new stuff is always good and I'd never noticed that log parser can query a file system!

Posted
Sorry; that's normally what causes the problem but you can test for the filename length (rather than full path) by checking the length of "filename" rather than "name" - code below. The echo statement shows the full path - you might need to know where the problem file is :-)

 

sComputer="."
Set oWMIService = GetObject("winmgmts:\\" & sComputer & "\root\cimv2")
set colItems = oWMIService.ExecQuery("select * from CIM_Datafile where drive='C:'")
for each item in ColItems
 if len(item.filename)>6 then
   wscript.echo item.name, item.FileName
 end if
next

 

Could you not do something like this where you have a variable and an inputbox and get it to prompt for how long you want the path for and where drive='C:'" etc use the vbscript browse folder option

 

VBScript Scripting Techniques: Browse Folder Dialog

 

That way you don't have to keep changing the Drive='C:'" etc it will just prompt for length and give you a browse window to browse and select where you want to scan

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...