0

I'm trying to make a .bat file that will run through all files in a given folder, measure the length of the file+path and if its longer than eg. 50 characters the file and path are written into a .txt-file.

I'm pretty much a n00b, so not to advanced, and somewhat well explained ;).

im writing into a file with

echo %File% > filer.txt

Where File should contain the file name and path.

Edit: Im sorry, I was perhaps a bit unclear in my description of the task it should preform. What I ment was, loop trough files and subfolders of a given folder. and return in the document the files with a path longer than 50 including Drive and Filetype.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • Oh god, if there is one thing batch isn't good at, it's measuring string length. I think your `not to advanced` is a terrible condition for this. – Dennis van Gils May 30 '16 at 12:26
  • Does path+file include the drive, so `C:\path\somefile`, or `path\somefile`? And does it contain the extension? – Dennis van Gils May 30 '16 at 12:32
  • 1
    check this [post by dbenham](http://ss64.org/viewtopic.php?pid=6478#p6478) – npocmaka May 30 '16 at 12:41
  • @npocmaka although it uses temporary files, I really like [this](http://stackoverflow.com/a/8566001/5022761) solution, mostly because it's single line. – Dennis van Gils May 30 '16 at 12:51

2 Answers2

2

As Dennis already noted, batch has no built-in solution for getting the lenght of the path. But if you only want to know, if a string is larger than n, just check, if there is a character at the n+1 position:

if "%string:~51,1%" neq "" echo %string% is longer than 50 characters

Much faster than counting characters.

Combined with Dennis' logic:

@echo off
setlocal EnableDelayedExpansion
(
  for %%g in ("*") do (
    set "File=%%~dpnxg"
    if "!File:~51,1!" neq "" echo !File!
  )
)>filer.txt

I put the whole for loop into another block to redirect only once to the output-file. this is much faster (>> would open the file, write one line and close the file, just to open it again etc...)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Why didn't I think of this. *silently judges himself* – Dennis van Gils May 30 '16 at 12:57
  • Im sorry, I was perhaps a bit unclear in my description of the task it should preform. What I ment was, loop trough files and subfolders of a given folder. and return in the document the files with a path longer than 50 including Drive and Filetype. – Susanne Kahl May 31 '16 at 08:58
  • just use the `for`parameter `/R`. See `for /?` for details. – Stephan May 31 '16 at 10:30
  • thanks I got it to work after Lunch, the code properly just needed a brake from me (se my answer to Dennis;) I'm a bit unsesure on the 'code !File:~51,1!" neq ' well I can guess moste of the meaning but I would like the more detailed form. – Susanne Kahl May 31 '16 at 11:13
  • `%var:~n,m%` extracts a substring starting at character `n` with length `m` (now, that I think about it, `n` starts with zero, so it should be `50` instead of `51`). There is an example in `set /?`. – Stephan May 31 '16 at 11:47
  • okay so just to be sure I understand it correctly for all files (%%g) in folder( set file=file including path If place 52 (51+1) is not "" write file into file.txt in the !File! what is the ! for? – Susanne Kahl May 31 '16 at 12:37
  • @SusanneKahl the `!` is used instead of `%` when using [delayed expansion](http://ss64.com/nt/delayedexpansion.html) – Dennis van Gils May 31 '16 at 12:50
1

You could use this:

@echo off
setlocal EnableDelayedExpansion
for %%g in ("*") do (
set "File=%%~dpnxg"
>x echo !File!&for %%? in (x) do set /a strlength=%%~z? - 2&del x
if !strlength! gtr 50 echo !File! >> filer.txt
)

This loops through all files in the current folder, and puts their drive, path, name and extension in the variable %File%. It then writes %File% to a temporary file x, and gets the stringlength from that, and deletes x. If the string length is greater than 50 it writes the name of the file to filer.txt.

You need the delayed expansion to work with variables inside loops.

Note, this code currently uses drive, path, filename and extension. Change the line set "File=%%~dpnxg" to change this behaviour ([d]rive, [p]ath, [n]ame, e[x]tension, the g is necesary)

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35
  • Fails if there is a _folder_ named `x` in the current directory: `Access denied` and then `4094d:\bat\x\*, Are you sure (Y/N)?` – JosefZ May 30 '16 at 15:30
  • Im sorry, I was perhaps a bit unclear in my description of the task it should preform. What I ment was, loop trough files and subfolders of a given folder. and return in the document the files with a path longer than 50 including Drive and Filetype. – Susanne Kahl May 31 '16 at 08:58
  • @SusanneKahl If it is supposed to loop through all files, even the ones in subfolders, you need to replace `for %%g in (` with `for /r %%g in (`, which also applies to @Stephan's answer – Dennis van Gils May 31 '16 at 09:05
  • I can only get the change to work on your code not on Stephans unfortunately. Can I get a short Introduction on what the lines do? now I got it to work, but dont know how ;) – Susanne Kahl May 31 '16 at 09:54
  • And Yes I also have the Access Denied on folder X but only in some (bigger) folders. – Susanne Kahl May 31 '16 at 10:37