根据子文件夹中的文件类型创建子文件夹,然后移动该类型的所有文件

时间:2019-07-11 04:42:57

标签: windows batch-file scripting

我的文档中有一个名为“杂项”的文件夹,该文件夹中有400个子文件夹,这变得有些混乱。每个子文件夹中都有不同的文件类型,我希望能够基于该文件类型在每个子文件夹中创建子文件夹,并将该文件类型的任何内容移动到新的子文件夹中。

所以我想要这样的东西:

C:\Users\Personal\Documents\Misc\
 - Subfolder1
  - PDF
  - PNG
  - PDF
- Subfolder2
  - XLSX
  - DOCX
  - PDF
- Subfolder3
  - M4A
  - MKV
  - PNG

每个子文件夹中的文件类型都不一致,这是我遇到麻烦的地方。这甚至有可能吗?

到目前为止,我一直在做的是进入每个文件夹并运行以下批处理文件

for %%a in (.) do md "%%~na PNG" &move "*.png" ".\%%~na PNG\"
for %%a in (.) do md "%%~na Documents" &move "*.docx" ".\%%~na Documents\"
for %%a in (.) do md "%%~na PDF" &move "*.pdf" ".\%%~na PDF\"
for %%a in (.) do md "%%~na Spreadsheets" &move "*.xlsx" ".\%%~na Spreadsheets\"

每次我遇到一个新的文件扩展名时,我都会添加一个新行。但是理想情况下,我只希望在文件扩展名位于文件夹中的情况下创建文件夹,并且我不必手动进入每个文件夹来运行它。

2 个答案:

答案 0 :(得分:1)

您的for loops无法访问文件系统,因为没有wildcards,因此它们仅迭代一次并在其元变量.中返回%%a

无论如何,不​​确定我是否正确理解了您想要的内容,但是请尝试以下代码:

@echo off
rem // Iterate through all immediate sub-directories of the target root directory:
for /D %%D in ("%UserProfile%\Documents\Misc\*") do (
    rem // Iterate through all files in each sub-directory:
    for %%F in ("%%~D\*.*") do (
        rem /* Remove the leading `.` from the name extension of the current file;
        rem    if the files has got no extension, this loop does not iterate: */
        for /F "tokens=* delims=." %%E in ("%%~xF") do (
            rem /* Create sub-directory from file name extension;
            rem    suppress errors if it already exists from previous iteration: */
            md "%%~D\%%E" 2> nul
            rem // Actually move the current file into the sub-directory:
            move /Y "%%~F" "%%~D\%%E"
        )
    )
)

答案 1 :(得分:0)

由于您对标签的使用仅限于,并且您已经接受的答案是,因此我更新了您已使用的标签并包括了此{{3} }根据您指定的要求的示例:

import { Inject, Injectable, Injector } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { switchMap } from 'rxjs/operators';
import { NbAuthToken } from '@nebular/auth';
import { NbAuthService } from '@nebular/auth';
import { NB_AUTH_TOKEN_INTERCEPTOR_FILTER } from '@nebular/auth';

@Injectable()
export class NgxAuthJWTInterceptor implements HttpInterceptor {

  constructor(private injector: Injector,
              @Inject(NB_AUTH_TOKEN_INTERCEPTOR_FILTER) protected filter) {
  }

  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    // do not intercept request whose urls are filtered by the injected filter
      if (!this.filter(req)) {
        return this.authService.isAuthenticatedOrRefresh()
          .pipe(
            switchMap(authenticated => {
              if (authenticated) {
                  return this.authService.getToken().pipe(
                    switchMap((token: NbAuthToken) => {
                      //const JWT = `Bearer ${token.getValue()}`;  <--- replace this line with the next
                      const JWT = `${token.getValue()}`;
                      req = req.clone({
                        setHeaders: {
                          Authorization: JWT,
                        },
                      });
                      return next.handle(req);
                    }),
                  )
              } else {
                 // Request is sent to server without authentication so that the client code
                 // receives the 401/403 error and can act as desired ('session expired', redirect to login, aso)
                return next.handle(req);
              }
            }),
          )
      } else {
      return next.handle(req);
    }
  }

  protected get authService(): NbAuthService {
    return this.injector.get(NbAuthService);
  }

}
相关问题