Windows – Using Windows Search for files containing “space”

batch-renamesearchwindows 10

I'm very sorry if this has been posted elsewhere, I've been searching for this answer for two days now, and my head hurts so bad. I had an old backup of my games directory made by Windows 7 a few years ago and I'm trying to restore it, but the backup got messed up when trying to write to disk. Windows got weird and placed "space" (not the word space, but spacebar space) before the extension to every file (hundreds of thousands of files!) which is now mixed with my current game directory. Because of the space character (is space even a character?), my games won't read the files.

I need to search for the files with space (am not able to do any special search because I don't know how to search for files containing "space"), then if possible, eliminate the space from that specific area of all files affected (it has to be that specific area as many files have space in them, but that was due to their original wording, i.e "ba room 56.xxx" which became "ba room 56 .xxx" after Windows got done with it. NOTE: space between 56 and the "." in last example.)

Please help?

Best Answer

Overview

  • Section 1 - Renaming files with Bulk Rename Utility.

  • Section 2 - Suggestions regarding grouping files to make processing easier.

  • Section 3 - Searching for files with spaces in their names just before the extension.

  • Section 4 - Exporting a list of those files as a text file.


Using Bulk Rename Utility

As I understand it, you were having some issues with Bulk Rename Utility.

Assuming your description is accurate (and there is simply one or more spaces as the last characters before the extension e.g. "ba room 56 .xxx"), then you can use the Trim function in Remove (5) without changing any options:

ex. Preview Results

Remove Spaces Before Extension - Bulk Rename Utility

To be clear:

  • Select all the items in the primary pane with Actions → Select All ( Ctrl+A )

  • Click the Trim checkbox in Remove (5).

You will note that only the names to be changed will appear in green. Likewise, this will only rename files, not folders. Once satisfied, click the Rename button in the lower right corner.

If desired, you can reset the entire renaming criteria window (in case you have been fooling around with other options or just feel cautious) with Actions → Reset All Renaming Criteria ( Ctrl+T ).

Including Sub-directories

Bulk Rename Utility normally operates on only a single root folder at a time. However, it is possible to include sub-directories:

ex. Replacements In Sub-Directories

Bulk Rename Utility Sub-directory Replacement

You can include these folders by using the Filters (12) section of the main window. Simply mark Subfolders:

ex. Filter Options

Bulk Rename Utility Folder Filters

I have also unchecked Folders to reduce visual clutter in the preview window (the use of Trim, as already noted, doesn't affect folders either way). After you have selected the proper filter options, use Select All, Trim and Rename as above.


Grouping Files With Spaces

In your case, Bulk Rename Utility has the ability to identify the correct file names to be modified without additional action (just point it at the proper directory containing those files). Files that don't match your criteria are automatically ignored.

However, as you point out, there are a large number of files and Bulk Rename Utility might have difficulty with processing all of them at once. As a suggestion, I might consider grouping files into temporary directories with about 50-100 files each.


While I do suggest a batch file to assist you below, it doesn't group files by a specific number. You will still have to create new directories and do that portion manually if you so choose. Similarly, it requires you to move and run it one folder at a time.


Batch File Sorting

To help generally ease the task of processing such a large number of files, you can sort out files containing spaces just before the extension with a batch file:

  • Open a new document in Windows Notepad and copy the following lines:

    if not exist %~dp0tempspace md %~dp0tempspace
    move /-y %~dp0"* .*" %~dp0tempspace
    pause
    
  • Choose Save As and under Save as type, choose All Files (*.*).

  • Change your document extension to .bat (Windows batch file) by typing e.g. findspaces.bat.

  • Click Save.

If done correctly, the file name should change to include the .bat extension and the icon for the new file should change to a box with gears in Explorer (rather than a sheet of paper).

Place this batch file in any folder you believe may have files with spaces just before the extension and double click to run it. Any matching files will moved to a new folder called e.g. tempspace. Repeat as necessary for additional folders and sub-directories, moving the batch file each time.

Batch File Notes

  • This batch file doesn't search sub-directories.

  • It moves all matching files to the given temporary directory.

  • You can replace e.g. tempspace with any folder name you like, but you should avoid spaces. Likewise, note the lack of space between %~dp0 and the folder name.

  • Instead of all files with spaces just before the name ("* .*"), you can use a specific extension (e.g."* .xxx"). Again, note the lack of space between %~dp0 and your search criteria, as well as the space between the first asterisk and the period.

  • While this shouldn't apply to saving new files, you may have to unhide extensions in Windows to change existing text files to a .bat extension.


Searching For Files

Since it may be helpful (but not necessarily needed) to know ahead of time which files contain spaces just before the extension, here are some thoughts regarding that topic...

There are a large number of utilities which could assist you. That said, regarding native Windows solutions, there is the find command line utility but I don't believe it has the capability you need. Similarly, I have never been a fan of Windows Search and I would be dubious about its helpfulness in your situation.

As an simple alternative, I would recommend Mythicsoft's Agent Ransack. It is a free file search program which you can download here. Note that you currently don't have to worry about "registering" the program as a home user (it will work regardless).

Once installed, open Agent Ransack and enter in the following in the main search window:

  • Type * .* in the File name field to search for all files with one or more leading spaces just before the extension. As with the batch file above, remember the space between the first asterisk and the period. You can, of course, specify an extension again as well e.g. * .xxx*.

  • Make sure the proper root folder (e.g. your game files folder) appears in the Look in field.

  • If you want to do a comprehensive search, you should check Search subfolders.

Press the Start button on the right to begin searching for files. Once the search is finished, you should see results similar to the following:

ex. Search Results

Agent Ransack Search Results

While my example image shows only a single directory, your real results will show full paths including sub-directories (assuming you marked Search subfolders).


Exporting A List Of Files

To get a list of the Agent Ransack search results as a text file, you can export them with FileExport results... or use the Export results icon on the Agent Ransack toolbar:

ex. Export Results

Exports Results Icon

In the Export window, make sure to save the results as a text file by:

  • Choosing File.

  • Setting an export path with an appropriate file name.

  • Selecting "Textfile" for the Format.

  • Making certain Save results for: All files is marked.

Click the Export button in the lower right once finished.

ex. Export Window

Export Results Dialog

Cleaning Things Up With Notepad++

Unfortunately, besides the full path and file name, the exported list comes with information you likely don't need e.g.:

C:\path\to\gamefiles\ba room 56 .xxx 1 KB XXX File 2/11/2018 11:56:11 PM [...]

To keep the path/file information and discard the rest, we can use a free text editor called Notepad++ (not to be confused with regular Windows Notepad). Simply select either the 32-bit or 64-bit installer on the Download page.

Once installed, open your e.g. SearchResults.txt file in Notepad++. From the menu, select Search → Replace... ( Ctrl+H ) to bring up the Replace dialog.

You can then use a regular expression (a special search pattern) to highlight the information you don't need. Type the following in the Find what : field:

\s\d+\s[A-Z].+$

Leave the Replace with : field blank. Make sure that the Match case, Wrap around and Regular expression options are all marked. The . matches newline option should be unchecked:

ex. Notepad++ Replace Dialog

Notepad++ Search Results Regular Expression

Once you are ready, click Replace All on the right. You can then save the edited file, assuming everything looks correct.

List Editing Notes

  • Please keep in mind that the regular expression I suggest for removing data could likely stand a little tweaking, but it should work correctly for text generated by Agent Ransack. Let me know if you encounter any issues.

  • This edited list could be the input for some other script. If you wish to use it in this fashion, it would probably be best to remove any blank lines. You can do this manually or Notepad++ can do this for you with Edit → Line Operations → Remove Empty Lines.

  • Removing blank lines also avoids an issue with quoting (normally invisible) lines that consist solely of spaces (assuming you wish to use the regular expression for quoting lines, below).

  • Quoting paths in this list should not be necessary in most instances (even if the paths in the list contain spaces). But if you do find you need each path in the list quoted for some reason, you can use Notepad++ for this as well. Simply open the Replace dialog with regular expressions again (with the same settings as above) but use (.+) in the Find what : field and "\1" in the Replace with : field:

Notepad++ Quoted Line Regular Expression

Related Question